Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AssetIndexMap.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.IO;
5 using System.Text;
6 using System.Text.RegularExpressions;
7 using SiliconStudio.Core.Storage;
8 using SiliconStudio.Core.IO;
9 using System.Collections.Generic;
10 using System.Linq;
11 
12 
13 namespace SiliconStudio.Core.Serialization.Assets
14 {
15  public sealed class AssetIndexMap : DictionaryStore<string, ObjectId>, IAssetIndexMap
16  {
17  private static Regex RegexEntry = new Regex(@"^(.*?)\s+(\w+)$");
18 
19  private AssetIndexMap()
20  : base(null)
21  {
22  }
23 
24  public static AssetIndexMap NewTool(string indexName = null)
25  {
26  if (string.IsNullOrWhiteSpace(indexName))
27  {
28  indexName = "index";
29  }
30 
31  var result = new AssetIndexMap();
32 
33  // Try to open with read-write
34  result.stream = VirtualFileSystem.OpenStream(
35  "/data/db/" + indexName,
36  VirtualFileMode.OpenOrCreate,
37  VirtualFileAccess.ReadWrite,
38  VirtualFileShare.ReadWrite);
39 
40  return result;
41  }
42 
43  public static AssetIndexMap Load(string indexFile = "/data/db/index", bool isReadOnly = false)
44  {
45  if (indexFile == null) throw new ArgumentNullException("indexFile");
46 
47  var result = new AssetIndexMap();
48 
49  var isAppDataWriteable = !isReadOnly;
50  if (isAppDataWriteable)
51  {
52  try
53  {
54  // Try to open with read-write
55  result.stream = VirtualFileSystem.OpenStream(
56  indexFile,
57  VirtualFileMode.OpenOrCreate,
58  VirtualFileAccess.ReadWrite,
59  VirtualFileShare.ReadWrite);
60  }
61  catch (UnauthorizedAccessException)
62  {
63  isAppDataWriteable = false;
64  }
65  }
66 
67  if (!isAppDataWriteable)
68  {
69  // Try to open read-only
70  result.stream = VirtualFileSystem.OpenStream(
71  indexFile,
72  VirtualFileMode.Open,
73  VirtualFileAccess.Read);
74  }
75 
76  result.LoadNewValues();
77 
78  return result;
79  }
80 
82  {
83  lock (lockObject)
84  {
85  return GetPendingItems(transaction);
86  }
87  }
88 
90  {
91  lock (lockObject)
92  {
93  return unsavedIdMap
94  .Select(x => new KeyValuePair<string, ObjectId>(x.Key, x.Value.Value))
95  .Concat(loadedIdMap.Where(x => !unsavedIdMap.ContainsKey(x.Key)))
96  .ToArray();
97  }
98  }
99 
100  protected override List<KeyValuePair<string, ObjectId>> ReadEntries(System.IO.Stream localStream)
101  {
102  var reader = new StreamReader(localStream, Encoding.UTF8);
103  string line;
104  var entries = new List<KeyValuePair<string, ObjectId>>();
105  while ((line = reader.ReadLine()) != null)
106  {
107  line = line.Trim();
108  if (line == string.Empty || line.StartsWith("#"))
109  continue;
110 
111  var match = RegexEntry.Match(line);
112  if (!match.Success)
113  {
114  throw new InvalidOperationException("Unable to read asset index entry [{0}]. Expecting: [path objectId]".ToFormat(line));
115  }
116 
117  var url = match.Groups[1].Value;
118  var objectIdStr = match.Groups[2].Value;
119 
120  ObjectId objectId;
121  if (!ObjectId.TryParse(objectIdStr, out objectId))
122  {
123  throw new InvalidOperationException("Unable to decode objectid [{0}] when reading asset index".ToFormat(objectIdStr));
124  }
125 
126  var entry = new KeyValuePair<string, ObjectId>(url, objectId);
127  entries.Add(entry);
128  }
129  return entries;
130  }
131 
132  protected override void WriteEntry(Stream stream, KeyValuePair<string, ObjectId> value)
133  {
134  var line = string.Format("{0} {1}\n", value.Key, value.Value);
135  var bytes = Encoding.UTF8.GetBytes(line);
136  stream.Write(bytes, 0, bytes.Length);
137  }
138  }
139 }
override List< KeyValuePair< string, ObjectId > > ReadEntries(System.IO.Stream localStream)
IEnumerable< KeyValuePair< string, ObjectId > > GetMergedIdMap()
System.Text.Encoding Encoding
override void WriteEntry(Stream stream, KeyValuePair< string, ObjectId > value)
A hash to uniquely identify data.
Definition: ObjectId.cs:13
static AssetIndexMap Load(string indexFile="/data/db/index", bool isReadOnly=false)
static bool TryParse(string input, out ObjectId result)
Tries to parse an ObjectId from a string.
Definition: ObjectId.cs:102
IEnumerable< KeyValuePair< string, ObjectId > > GetTransactionIdMap()
static AssetIndexMap NewTool(string indexName=null)