[longomatch] Add a new function to load safely between different formats



commit 9cc97c6b454e71a0ef62a9349e441d37fd587182
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sun Jan 26 01:46:00 2014 +0100

    Add a new function to load safely between different formats

 LongoMatch.Core/Common/PlayList.cs                 |    2 +-
 LongoMatch.Core/Common/SerializableObject.cs       |   13 +++++++++++++
 .../Store/Templates/CategoriesTemplate.cs          |    2 +-
 .../Store/Templates/SubCategoryTemplate.cs         |    2 +-
 LongoMatch.Core/Store/Templates/TeamTemplate.cs    |    2 +-
 5 files changed, 17 insertions(+), 4 deletions(-)
---
diff --git a/LongoMatch.Core/Common/PlayList.cs b/LongoMatch.Core/Common/PlayList.cs
index 228f9e6..4c48277 100644
--- a/LongoMatch.Core/Common/PlayList.cs
+++ b/LongoMatch.Core/Common/PlayList.cs
@@ -73,7 +73,7 @@ namespace LongoMatch.Common
                        if(!System.IO.File.Exists(filePath))
                                pl = new PlayList();
                        else
-                               pl = SerializableObject.Load<PlayList>(filePath);
+                               pl = SerializableObject.LoadSafe<PlayList>(filePath);
                        pl.Filename = filename;
                        return pl; 
                }
diff --git a/LongoMatch.Core/Common/SerializableObject.cs b/LongoMatch.Core/Common/SerializableObject.cs
index 521cab6..d32f466 100644
--- a/LongoMatch.Core/Common/SerializableObject.cs
+++ b/LongoMatch.Core/Common/SerializableObject.cs
@@ -73,6 +73,19 @@ namespace LongoMatch.Common
                                return Load<T> (stream, type);
                        }
                }
+               
+               public static T LoadSafe<T>(string filepath) {
+               
+                       Stream stream = new FileStream (filepath, FileMode.Open,
+                                                      FileAccess.Read, FileShare.Read);
+                       using (stream) {
+                               try {
+                                       return Load<T> (stream, SerializationType.Binary);
+                               } catch {
+                                       return Load<T> (stream, SerializationType.Xml);
+                               }
+                       }
+               }
        }
 }
 
diff --git a/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs 
b/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
index 16f7fb4..395efec 100644
--- a/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/CategoriesTemplate.cs
@@ -184,7 +184,7 @@ namespace LongoMatch.Store.Templates
                }
 
                public static Categories Load(string filePath) {
-                       return SerializableObject.Load<Categories>(filePath);
+                       return SerializableObject.LoadSafe<Categories>(filePath);
                }
 
                public static Categories DefaultTemplate(int count) {
diff --git a/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs 
b/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs
index 4cabc20..465bfdc 100644
--- a/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/SubCategoryTemplate.cs
@@ -47,7 +47,7 @@ namespace LongoMatch.Store.Templates
                }
 
                public static SubCategoryTemplate Load(string filePath) {
-                       return SerializableObject.Load<SubCategoryTemplate>(filePath);
+                       return SerializableObject.LoadSafe<SubCategoryTemplate>(filePath);
                }
                
                public static SubCategoryTemplate DefaultTemplate (int not_used) {
diff --git a/LongoMatch.Core/Store/Templates/TeamTemplate.cs b/LongoMatch.Core/Store/Templates/TeamTemplate.cs
index 42fcf29..4b67795 100644
--- a/LongoMatch.Core/Store/Templates/TeamTemplate.cs
+++ b/LongoMatch.Core/Store/Templates/TeamTemplate.cs
@@ -87,7 +87,7 @@ namespace LongoMatch.Store.Templates
                }
 
                public static TeamTemplate Load(string filePath) {
-                       return SerializableObject.Load<TeamTemplate>(filePath);
+                       return SerializableObject.LoadSafe<TeamTemplate>(filePath);
                }
 
                public static TeamTemplate DefaultTemplate(int playersCount) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]