public ProxyTextItemModel(BlockDefinition b) { _def = b; var font = new Font { Size = b.text.size, Src = System.IO.Path.Combine(b.Manager.RootPath, b.text.font ?? "") }; }
public object Deserialize(string fileName) { var serializer = new XmlSerializer(typeof(game)); directory = new FileInfo(fileName).Directory.FullName; game g = null; var fileHash = ""; using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { g = (game)serializer.Deserialize(fs); if (g == null) { return null; } using (MD5 md5 = new MD5CryptoServiceProvider()) { fs.Seek(0, SeekOrigin.Begin); byte[] retVal = md5.ComputeHash(fs); fileHash = BitConverter.ToString(retVal).Replace("-", ""); // hex string } } var ret = new Game() { Id = new Guid(g.id), Name = g.name, CardSizes = new Dictionary<string, CardSize>(), GameBoards = new Dictionary<string, GameBoard>(), Version = Version.Parse(g.version), CustomProperties = new List<PropertyDef>(), DeckSections = new Dictionary<string, DeckSection>(), SharedDeckSections = new Dictionary<string, DeckSection>(), GlobalVariables = new List<GlobalVariable>(), Authors = g.authors.Split(',').ToList(), Description = g.description, Filename = fileName, ChatFont = new Font(), ContextFont = new Font(), NoteFont = new Font(), DeckEditorFont = new Font(), GameUrl = g.gameurl, IconUrl = g.iconurl, Tags = g.tags.Split(' ').ToList(), OctgnVersion = Version.Parse(g.octgnVersion), Variables = new List<Variable>(), MarkerSize = g.markersize, Documents = new List<Document>(), Sounds = new Dictionary<string, GameSound>(), FileHash = fileHash, Events = new Dictionary<string, GameEvent[]>(), InstallPath = directory, UseTwoSidedTable = g.usetwosidedtable == boolean.True ? true : false, NoteBackgroundColor = g.noteBackgroundColor, NoteForegroundColor = g.noteForegroundColor, ScriptVersion = Version.Parse(g.scriptVersion), Scripts = new List<string>(), Modes = new List<GameMode>(), }; var defSize = new CardSize(); defSize.Name = "Default"; defSize.Back = String.IsNullOrWhiteSpace(g.card.back) ? "pack://application:,,,/Resources/Back.jpg" : Path.Combine(directory, g.card.back); defSize.Front = String.IsNullOrWhiteSpace(g.card.front) ? "pack://application:,,,/Resources/Front.jpg" : Path.Combine(directory, g.card.front); defSize.Height = int.Parse(g.card.height); defSize.Width = int.Parse(g.card.width); defSize.CornerRadius = int.Parse(g.card.cornerRadius); defSize.BackHeight = int.Parse(g.card.backHeight); defSize.BackWidth = int.Parse(g.card.backWidth); defSize.BackCornerRadius = int.Parse(g.card.backCornerRadius); if (defSize.BackHeight < 0) defSize.BackHeight = defSize.Height; if (defSize.BackWidth < 0) defSize.BackWidth = defSize.Width; if (defSize.BackCornerRadius < 0) defSize.BackCornerRadius = defSize.CornerRadius; ret.CardSizes.Add("Default", defSize); ret.CardSize = ret.CardSizes["Default"]; #region variables if (g.variables != null) { foreach (var item in g.variables) { ret.Variables.Add(new Variable { Name = item.name, Global = bool.Parse(item.global.ToString()), Reset = bool.Parse(item.reset.ToString()), Default = int.Parse(item.@default) }); } } #endregion variables #region table ret.Table = this.DeserialiseGroup(g.table, 0); #endregion table #region gameBoards if (g.gameboards == null) { var defBoard = new GameBoard(); defBoard.Name = "Default"; defBoard.Source = g.table.board == null ? null : Path.Combine(directory, g.table.board); defBoard.XPos = g.table.boardPosition == null ? 0 : double.Parse(g.table.boardPosition.Split(',')[0]); defBoard.YPos = g.table.boardPosition == null ? 0 : double.Parse(g.table.boardPosition.Split(',')[1]); defBoard.Width = g.table.boardPosition == null ? 0 : double.Parse(g.table.boardPosition.Split(',')[2]); defBoard.Height = g.table.boardPosition == null ? 0 : double.Parse(g.table.boardPosition.Split(',')[3]); ret.GameBoards.Add("Default", defBoard); } else { var defBoard = new GameBoard(); defBoard.Name = "Default"; defBoard.Source = Path.Combine(directory, g.gameboards.src); defBoard.XPos = int.Parse(g.gameboards.x); defBoard.YPos = int.Parse(g.gameboards.y); defBoard.Width = int.Parse(g.gameboards.width); defBoard.Height = int.Parse(g.gameboards.height); ret.GameBoards.Add("Default", defBoard); foreach (var board in g.gameboards.gameboard) { var b = new GameBoard(); b.Name = board.name; b.XPos = int.Parse(board.x); b.YPos = int.Parse(board.y); b.Width = int.Parse(board.width); b.Height = int.Parse(board.height); b.Source = Path.Combine(directory, board.src); ret.GameBoards.Add(board.name, b); } } #endregion gameBoards #region shared if (g.shared != null) { var player = new GlobalPlayer { Counters = new List<Counter>(), Groups = new List<Group>(), IndicatorsFormat = g.shared.summary }; var curCounter = 1; var curGroup = 2; if (g.shared.counter != null) { foreach (var i in g.shared.counter) { (player.Counters as List<Counter>).Add( new Counter { Id = (byte)curCounter, Name = i.name, Icon = Path.Combine(directory, i.icon ?? ""), Reset = bool.Parse(i.reset.ToString()), Start = int.Parse(i.@default) }); curCounter++; } } if (g.shared.group != null) { foreach (var i in g.shared.group) { (player.Groups as List<Group>).Add(this.DeserialiseGroup(i, curGroup)); curGroup++; } } ret.GlobalPlayer = player; } #endregion shared #region Player if (g.player != null) { var player = new Player { Groups = new List<Group>(), GlobalVariables = new List<GlobalVariable>(), Counters = new List<Counter>(), IndicatorsFormat = g.player.summary }; var curCounter = 1; var curGroup = 2; foreach (var item in g.player.Items) { if (item is counter) { var i = item as counter; (player.Counters as List<Counter>) .Add(new Counter { Id = (byte)curCounter, Name = i.name, Icon = Path.Combine(directory, i.icon ?? ""), Reset = bool.Parse(i.reset.ToString()), Start = int.Parse(i.@default) }); curCounter++; } else if (item is gamePlayerGlobalvariable) { var i = item as gamePlayerGlobalvariable; var to = new GlobalVariable { Name = i.name, Value = i.value, DefaultValue = i.value }; (player.GlobalVariables as List<GlobalVariable>).Add(to); } else if (item is hand) { player.Hand = this.DeserialiseGroup(item as hand, 1); } else if (item is group) { var i = item as group; (player.Groups as List<Group>).Add(this.DeserialiseGroup(i, curGroup)); curGroup++; } } ret.Player = player; } #endregion Player #region documents if (g.documents != null) { foreach (var doc in g.documents) { var d = new Document(); d.Icon = Path.Combine(directory, doc.icon); d.Name = doc.name; d.Source = Path.Combine(directory, doc.src); ret.Documents.Add(d); } } #endregion documents #region sounds if (g.sounds != null) { foreach (var sound in g.sounds) { var s = new GameSound(); s.Gameid = ret.Id; s.Name = sound.name; s.Src = Path.Combine(directory, sound.src); ret.Sounds.Add(s.Name.ToLowerInvariant(), s); } } #endregion sounds #region deck if (g.deck != null) { foreach (var ds in g.deck) { ret.DeckSections.Add(ds.name, new DeckSection { Group = ds.group, Name = ds.name, Shared = false }); } } if (g.sharedDeck != null) { foreach (var s in g.sharedDeck) { ret.SharedDeckSections.Add(s.name, new DeckSection { Group = s.group, Name = s.name, Shared = true }); } } #endregion deck #region card if (g.card != null) { if (g.card.property != null) { foreach (var prop in g.card.property) { var pd = new PropertyDef(); pd.Name = prop.name; switch (prop.textKind) { case propertyDefTextKind.Free: pd.TextKind = PropertyTextKind.FreeText; break; case propertyDefTextKind.Enum: pd.TextKind = PropertyTextKind.Enumeration; break; case propertyDefTextKind.Tokens: pd.TextKind = PropertyTextKind.Tokens; break; default: throw new ArgumentOutOfRangeException(); } pd.Type = (PropertyType) Enum.Parse(typeof (PropertyType), prop.type.ToString()); pd.IgnoreText = bool.Parse(prop.ignoreText.ToString()); pd.Hidden = bool.Parse(prop.hidden); ret.CustomProperties.Add(pd); } } if (g.card.size != null) { foreach (var size in g.card.size) { var cs = new CardSize(); cs.Name = size.name; cs.Width = int.Parse(size.width); cs.Height = int.Parse(size.height); cs.CornerRadius = int.Parse(size.cornerRadius); cs.BackWidth = int.Parse(size.backWidth); cs.BackHeight = int.Parse(size.backHeight); cs.BackCornerRadius = int.Parse(size.backCornerRadius); if (cs.BackHeight < 0) cs.BackHeight = ret.CardSize.Height; if (cs.BackWidth < 0) cs.BackWidth = ret.CardSize.Width; if (cs.BackCornerRadius < 0) cs.BackCornerRadius = ret.CardSize.CornerRadius; cs.Front = String.IsNullOrWhiteSpace(size.front) ? "pack://application:,,,/Resources/Front.jpg" : Path.Combine(directory, size.front); cs.Back = String.IsNullOrWhiteSpace(size.back) ? "pack://application:,,,/Resources/Back.jpg" : Path.Combine(directory, size.back); ret.CardSizes.Add(cs.Name,cs); } } } var namepd = new PropertyDef(); namepd.Name = "Name"; namepd.TextKind = PropertyTextKind.FreeText; namepd.Type = PropertyType.String; ret.CustomProperties.Add(namepd); #endregion card #region fonts if (g.fonts != null) { foreach (gameFont font in g.fonts) { Font f = new Font(); f.Src = Path.Combine(directory, font.src ?? "").Replace("/", "\\"); f.Size = (int)font.size; switch (font.target) { case fonttarget.chat: ret.ChatFont = f; break; case fonttarget.context: ret.ContextFont = f; break; case fonttarget.deckeditor: ret.DeckEditorFont = f; break; case fonttarget.notes: ret.NoteFont = f; break; } } } #endregion fonts #region scripts if (g.scripts != null) { foreach (var s in g.scripts) { ret.Scripts.Add(s.src); var coll = Def.Config .DefineCollection<GameScript>("Scripts") .OverrideRoot(x => x.Directory("GameDatabase")) .SetPart(x => x.Directory(ret.Id.ToString())); var pathParts = s.src.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); for (var index = 0; index < pathParts.Length; index++) { var i = index; if (i == pathParts.Length - 1) coll.SetPart(x => x.File(pathParts[i])); else coll.SetPart(x => x.Directory(pathParts[i])); } coll.SetSerializer(new GameScriptSerializer(ret.Id)); } } #endregion scripts #region events if (g.events != null) { foreach (var e in g.events) { var eve = new GameEvent() { Name = e.name.Clone() as string, PythonFunction = e.action.Clone() as string }; if (ret.Events.ContainsKey(e.name)) { var narr = ret.Events[e.name]; Array.Resize(ref narr, narr.Length + 1); narr[narr.Length - 1] = eve; ret.Events[e.name] = narr; } else { ret.Events.Add(e.name, new GameEvent[1] { eve }); } } } #endregion Events #region proxygen if (g.proxygen != null && g.proxygen.definitionsrc != null) { ret.ProxyGenSource = g.proxygen.definitionsrc; var coll = Def.Config.DefineCollection<ProxyDefinition>("Proxies") .OverrideRoot(x => x.Directory("GameDatabase")) .SetPart(x => x.Directory(ret.Id.ToString())); //.SetPart(x => x.Property(y => y.Key)); var pathParts = g.proxygen.definitionsrc.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); for (var index = 0; index < pathParts.Length; index++) { var i = index; if (i == pathParts.Length - 1) coll.SetPart(x => x.File(pathParts[i])); else coll.SetPart(x => x.Directory(pathParts[i])); } coll.SetSerializer(new ProxyGeneratorSerializer(ret.Id, g.proxygen)); } #endregion proxygen #region globalvariables if (g.globalvariables != null) { foreach (var item in g.globalvariables) { ret.GlobalVariables.Add(new GlobalVariable { Name = item.name, Value = item.value, DefaultValue = item.value }); } } #endregion globalvariables #region hash #endregion hash #region GameModes if (g.gameModes != null) { foreach (var mode in g.gameModes) { var m = new GameMode(); m.Name = mode.name; m.PlayerCount = mode.playerCount; m.ShortDescription = mode.shortDescription; m.Image = Path.Combine(directory, mode.image); m.UseTwoSidedTable = mode.usetwosidedtable; ret.Modes.Add(m); } } if (ret.Modes.Count == 0) { var gm = new GameMode(); gm.Name = "Default"; gm.PlayerCount = 2; gm.ShortDescription = "Default Game Mode"; ret.Modes.Add(gm); } #endregion GameModes return ret; }
public object Deserialize(string fileName) { var serializer = new XmlSerializer(typeof(game)); directory = new FileInfo(fileName).Directory.FullName; game g = null; var fileHash = ""; using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { g = (game)serializer.Deserialize(fs); if (g == null) { return null; } using (MD5 md5 = new MD5CryptoServiceProvider()) { fs.Seek(0, SeekOrigin.Begin); byte[] retVal = md5.ComputeHash(fs); fileHash = BitConverter.ToString(retVal).Replace("-", ""); // hex string } } var ret = new Game() { Id = new Guid(g.id), Name = g.name, CardBack = String.IsNullOrWhiteSpace(g.card.back) ? "pack://application:,,,/Resources/Back.jpg" : Path.Combine(directory, g.card.back), CardFront = String.IsNullOrWhiteSpace(g.card.front) ? "pack://application:,,,/Resources/Front.jpg" : Path.Combine(directory, g.card.front), CardHeight = int.Parse(g.card.height), CardWidth = int.Parse(g.card.width), CardCornerRadius = int.Parse(g.card.cornerRadius), Version = Version.Parse(g.version), CustomProperties = new List<PropertyDef>(), DeckSections = new Dictionary<string, DeckSection>(), SharedDeckSections = new Dictionary<string, DeckSection>(), GlobalVariables = new List<GlobalVariable>(), Authors = g.authors.Split(',').ToList(), Description = g.description, Filename = fileName, Fonts = new List<Font>(), GameUrl = g.gameurl, IconUrl = g.iconurl, Tags = g.tags.Split(' ').ToList(), OctgnVersion = Version.Parse(g.octgnVersion), Variables = new List<Variable>(), MarkerSize = g.markersize, Documents = new List<Document>(), Sounds = new Dictionary<string,GameSound>(), FileHash=fileHash }; #region variables if (g.variables != null) { foreach (var item in g.variables) { ret.Variables.Add(new Variable { Name = item.name, Global = bool.Parse(item.global.ToString()), Reset = bool.Parse(item.reset.ToString()), Default = int.Parse(item.@default) }); } } #endregion variables #region table ret.Table = this.DeserialiseGroup(g.table, 0); #endregion table #region shared if (g.shared != null) { var player = new GlobalPlayer { Counters = new List<Counter>(), Groups = new List<Group>() }; var curCounter = 1; var curGroup = 1; if (g.shared.counter != null) { foreach (var i in g.shared.counter) { (player.Counters as List<Counter>).Add( new Counter { Id = (byte)curCounter, Name = i.name, Icon = Path.Combine(directory, i.icon ?? ""), Reset = bool.Parse(i.reset.ToString()), Start = int.Parse(i.@default) }); curCounter++; } } if (g.shared.group != null) { foreach (var i in g.shared.group) { (player.Groups as List<Group>).Add(this.DeserialiseGroup(i, curGroup)); curGroup++; } } ret.GlobalPlayer = player; } #endregion shared #region Player if (g.player != null) { var player = new Player { Groups = new List<Group>(), GlobalVariables = new List<GlobalVariable>(), Counters = new List<Counter>(), IndicatorsFormat = g.player.summary }; var curCounter = 1; var curGroup = 1; foreach (var item in g.player.Items) { if (item is counter) { var i = item as counter; (player.Counters as List<Counter>) .Add(new Counter { Id = (byte)curCounter, Name = i.name, Icon = Path.Combine(directory, i.icon ?? ""), Reset = bool.Parse(i.reset.ToString()), Start = int.Parse(i.@default) }); curCounter++; } else if (item is gamePlayerGlobalvariable) { var i = item as gamePlayerGlobalvariable; var to = new GlobalVariable { Name = i.name, Value = i.value, DefaultValue = i.value }; (player.GlobalVariables as List<GlobalVariable>).Add(to); } else if (item is hand) { player.Hand = this.DeserialiseGroup(item as hand, 0); } else if (item is group) { var i = item as group; (player.Groups as List<Group>).Add(this.DeserialiseGroup(i, curGroup)); curGroup++; } } ret.Player = player; } #endregion Player #region documents if (g.documents != null) { foreach (var doc in g.documents) { var d = new Document(); d.Icon = Path.Combine(directory, doc.icon); d.Name = doc.name; d.Source = Path.Combine(directory, doc.src); ret.Documents.Add(d); } } #endregion documents #region sounds if (g.sounds != null) { foreach (var sound in g.sounds) { var s = new GameSound(); s.Gameid = ret.Id; s.Name = sound.name; s.Src = Path.Combine(directory, sound.src); ret.Sounds.Add(s.Name.ToLowerInvariant(),s); } } #endregion sounds #region deck if (g.deck != null) { foreach (var ds in g.deck) { ret.DeckSections.Add(ds.name, new DeckSection { Group = ds.group, Name = ds.name }); } } if (g.sharedDeck != null) { foreach (var s in g.sharedDeck) { ret.SharedDeckSections.Add(s.name, new DeckSection { Group = s.group, Name = s.name }); } } #endregion deck #region card if (g.card != null && g.card.property != null) { foreach (var prop in g.card.property) { var pd = new PropertyDef(); pd.Name = prop.name; switch (prop.textKind) { case propertyDefTextKind.Free: pd.TextKind = PropertyTextKind.FreeText; break; case propertyDefTextKind.Enum: pd.TextKind = PropertyTextKind.Enumeration; break; case propertyDefTextKind.Tokens: pd.TextKind = PropertyTextKind.Tokens; break; default: throw new ArgumentOutOfRangeException(); } pd.Type = (PropertyType)Enum.Parse(typeof(PropertyType), prop.type.ToString()); pd.IgnoreText = bool.Parse(prop.ignoreText.ToString()); pd.Hidden = bool.Parse(prop.hidden); ret.CustomProperties.Add(pd); } } var namepd = new PropertyDef(); namepd.Name = "Name"; namepd.TextKind = PropertyTextKind.FreeText; namepd.Type = PropertyType.String; ret.CustomProperties.Add(namepd); #endregion card #region fonts if (g.fonts != null) { foreach (gameFont font in g.fonts) { Font f = new Font(); f.Src = Path.Combine(directory, font.src ?? ""); f.Size = (int)font.size; switch (font.target) { case fonttarget.chat: f.Target = Enum.Parse(typeof(fonttarget), "chat").ToString(); break; case fonttarget.context: f.Target = Enum.Parse(typeof(fonttarget), "context").ToString(); break; case fonttarget.deckeditor: f.Target = Enum.Parse(typeof(fonttarget), "deckeditor").ToString(); break; } ret.Fonts.Add(f); } } #endregion fonts #region scripts if (g.scripts != null) { foreach (var s in g.scripts) { var coll = Def.Config .DefineCollection<GameScript>("Scripts") .OverrideRoot(x => x.Directory("GameDatabase")) .SetPart(x => x.Directory(ret.Id.ToString())); var pathParts = s.src.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); for (var index = 0; index < pathParts.Length; index++) { var i = index; if (i == pathParts.Length - 1) coll.SetPart(x => x.File(pathParts[i])); else coll.SetPart(x => x.Directory(pathParts[i])); } coll.SetSerializer(new GameScriptSerializer(ret.Id)); } } #endregion scripts #region proxygen if (g.proxygen != null) { var coll = Def.Config.DefineCollection<ProxyDefinition>("Proxies") .OverrideRoot(x => x.Directory("GameDatabase")) .SetPart(x => x.Directory(ret.Id.ToString())); //.SetPart(x => x.Property(y => y.Key)); var pathParts = g.proxygen.definitionsrc.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); for (var index = 0; index < pathParts.Length; index++) { var i = index; if (i == pathParts.Length - 1) coll.SetPart(x => x.File(pathParts[i])); else coll.SetPart(x => x.Directory(pathParts[i])); } coll.SetSerializer(new ProxyGeneratorSerializer(ret.Id, g.proxygen)); } #endregion proxygen #region globalvariables if (g.globalvariables != null) { foreach (var item in g.globalvariables) { ret.GlobalVariables.Add(new GlobalVariable { Name = item.name, Value = item.value, DefaultValue = item.value }); } } #endregion globalvariables #region hash #endregion hash return ret; }