Exemplo n.º 1
0
        public byte[] Serialize(object obj)
        {
            if ((obj is Game) == false)
                throw new InvalidOperationException("obj must be typeof Game");

            var game = obj as Game;
            var rootPath = new DirectoryInfo(game.InstallPath).FullName;

            var save = new game();
            save.id = game.Id.ToString();
            save.name = game.Name;
            save.card = new gameCard();
            save.card.back = game.CardSize.Back;
            save.card.front = game.CardSize.Front;
            save.card.height = game.CardSize.Height.ToString();
            save.card.width = game.CardSize.Width.ToString();
            save.card.cornerRadius = game.CardSize.CornerRadius.ToString();
            save.version = game.Version.ToString();
            save.authors = string.Join(",", game.Authors);
            save.description = game.Description;
            save.gameurl = game.GameUrl;
            save.iconurl = game.IconUrl;
            save.tags = string.Join(" ", game.Tags);
            save.octgnVersion = game.OctgnVersion.ToString();
            save.markersize = game.MarkerSize;
            save.usetwosidedtable = game.UseTwoSidedTable ? boolean.True : boolean.False;
            save.noteBackgroundColor = game.NoteBackgroundColor;
            save.noteForegroundColor = game.NoteForegroundColor;
            save.scriptVersion = game.ScriptVersion == null ? null : game.ScriptVersion.ToString();

            #region Variables

            save.variables = (game.Variables ?? new List<Variable>()).Select(x => new gameVariable()
            {
                @default = x.Default.ToString(),
                global = x.Global ? boolean.True : boolean.False,
                name = x.Name,
                reset = x.Reset ? boolean.True : boolean.False,
            }).ToArray();

            #endregion
            #region table
            save.table = SerializeGroup(game.Table, rootPath);
            #endregion table
            #region gameBoards
            if (game.GameBoards != null)
            {
                var boardList = new List<gameBoardDef>();
                foreach (var b in game.GameBoards)
                {
                    var board = new gameBoardDef();
                    board.name = b.Value.Name;
                    board.x = b.Value.XPos.ToString();
                    board.y = b.Value.YPos.ToString();
                    board.width = b.Value.Width.ToString();
                    board.height = b.Value.Height.ToString();
                    board.src = (b.Value.Source ?? "").Replace(rootPath, "");
                    boardList.Add(board);
                }
                save.gameboards.gameboard = boardList.ToArray();
            }
            #endregion gameBoards
            #region shared

            if (game.GlobalPlayer != null)
            {
                var gs = new gameShared();
                var clist = new List<counter>();
                foreach (var counter in game.GlobalPlayer.Counters)
                {
                    var c = new counter();
                    c.name = counter.Name;
                    c.icon = (counter.Icon ?? "").Replace(rootPath, "");
                    c.reset = counter.Reset ? boolean.True : boolean.False;
                    c.@default = counter.Start.ToString();
                    clist.Add(c);
                }
                var glist = new List<group>();
                foreach (var group in game.GlobalPlayer.Groups)
                {
                    glist.Add(SerializeGroup(group, rootPath));
                }
                gs.group = glist.ToArray();
                gs.counter = clist.ToArray();
                save.shared = gs;
            }
            #endregion shared
            #region player
            if (game.Player != null)
            {
                var player = new gamePlayer();
                var ilist = new List<object>();
                foreach (var counter in game.Player.Counters)
                {
                    var c = new counter();
                    c.name = counter.Name;
                    c.icon = (counter.Icon ?? "").Replace(rootPath, "");
                    c.reset = counter.Reset ? boolean.True : boolean.False;
                    c.@default = counter.Start.ToString();
                    ilist.Add(c);
                }
                foreach (var v in game.Player.GlobalVariables)
                {
                    var gv = new gamePlayerGlobalvariable();
                    gv.name = v.Name;
                    gv.value = v.Value;
                    ilist.Add(gv);
                }
                ilist.Add(SerializeGroup(game.Player.Hand, rootPath));
                foreach (var g in game.Player.Groups)
                {
                    ilist.Add(SerializeGroup(g, rootPath));
                }
                player.Items = ilist.ToArray();
                player.summary = game.Player.IndicatorsFormat;
                save.player = player;
            }
            #endregion player
            #region documents

            if (game.Documents != null)
            {
                var docList = new List<gameDocument>();
                foreach (var d in game.Documents)
                {
                    var doc = new gameDocument();
                    doc.icon = (d.Icon ?? "").Replace(rootPath, "");
                    doc.name = d.Name;
                    doc.src = (d.Source ?? "").Replace(rootPath, "");
                    docList.Add(doc);
                }
                save.documents = docList.ToArray();
            }

            #endregion documents
            #region sounds

            if (game.Sounds != null)
            {
                var soundList = new List<gameSound>();
                foreach (var d in game.Sounds)
                {
                    var doc = new gameSound();
                    doc.name = d.Value.Name;
                    doc.src = (d.Value.Src ?? "").Replace(rootPath, "");
                    soundList.Add(doc);
                }
                save.sounds = soundList.ToArray();
            }

            #endregion sounds
            #region deck

            if (game.DeckSections != null)
            {
                var dl = new List<deckSection>();
                foreach (var s in game.DeckSections)
                {
                    var sec = new deckSection();
                    sec.name = s.Value.Name;
                    sec.group = s.Value.Group;
                    dl.Add(sec);
                }
                save.deck = dl.ToArray();
            }

            if (game.SharedDeckSections != null)
            {
                var dl = new List<deckSection>();
                foreach (var s in game.SharedDeckSections)
                {
                    var sec = new deckSection();
                    sec.name = s.Value.Name;
                    sec.group = s.Value.Group;
                    dl.Add(sec);
                }
                save.sharedDeck = dl.ToArray();
            }
            #endregion deck
            #region card

            if (game.CustomProperties != null)
            {
                var pl = new List<propertyDef>();
                foreach (var prop in game.CustomProperties)
                {
                    if (prop.Name == "Name") continue;
                    var pd = new propertyDef();
                    pd.name = prop.Name;
                    pd.type = (propertyDefType)Enum.Parse(typeof(propertyDefType), prop.Type.ToString());
                    pd.hidden = prop.Hidden.ToString();
                    pd.ignoreText = prop.IgnoreText ? boolean.True : boolean.False;
                    switch (prop.TextKind)
                    {
                        case PropertyTextKind.FreeText:
                            pd.textKind = propertyDefTextKind.Free;
                            break;
                        case PropertyTextKind.Enumeration:
                            pd.textKind = propertyDefTextKind.Enum;
                            break;
                        case PropertyTextKind.Tokens:
                            pd.textKind = propertyDefTextKind.Tokens;
                            break;
                    }
                    pl.Add(pd);
                }
                save.card.property = pl.ToArray();
            }
            #endregion card
            #region fonts

            var flist = new List<gameFont>();
            if (game.ChatFont.IsSet())
            {
                var f = new gameFont();
                f.src = (game.ChatFont.Src ?? "").Replace(rootPath, "");
                f.size = (uint)game.ChatFont.Size;
                f.target = fonttarget.chat;
                flist.Add(f);
            }
            if (game.ContextFont.IsSet())
            {
                var f = new gameFont();
                f.src = (game.ContextFont.Src ?? "").Replace(rootPath, "");
                f.size = (uint)game.ContextFont.Size;
                f.target = fonttarget.context;
                flist.Add(f);
            }
            if (game.NoteFont.IsSet())
            {
                var f = new gameFont();
                f.src = (game.NoteFont.Src ?? "").Replace(rootPath, "");
                f.size = (uint)game.NoteFont.Size;
                f.target = fonttarget.notes;
                flist.Add(f);
            }
            if (game.DeckEditorFont.IsSet())
            {
                var f = new gameFont();
                f.src = (game.DeckEditorFont.Src ?? "").Replace(rootPath, "");
                f.size = (uint)game.DeckEditorFont.Size;
                f.target = fonttarget.deckeditor;
                flist.Add(f);
            }
            save.fonts = flist.ToArray();
            #endregion fonts
            #region scripts

            if (game.Scripts != null)
            {
                var scriptList = new List<gameScript>();
                foreach (var script in game.Scripts)
                {
                    var f = new gameScript();
                    f.src = script;
                    scriptList.Add(f);
                }
                save.scripts = scriptList.ToArray();
            }

            #endregion scripts
            #region events

            if (game.Events != null)
            {
                var eventList = new List<gameEvent>();
                foreach (var e in game.Events.SelectMany(x => x.Value))
                {
                    var eve = new gameEvent();
                    eve.name = e.Name;
                    eve.action = e.PythonFunction;
                    eventList.Add(eve);
                }
                save.events = eventList.ToArray();
            }

            #endregion events
            #region proxygen

            save.proxygen = new gameProxygen();
            save.proxygen.definitionsrc = game.ProxyGenSource;
            #endregion proxygen
            #region globalvariables

            if (game.GlobalVariables != null)
            {
                var gllist = new List<gameGlobalvariable>();
                foreach (var gv in game.GlobalVariables)
                {
                    var v = new gameGlobalvariable();
                    v.name = gv.Name;
                    v.value = gv.Value;
                    gllist.Add(v);
                }
                save.globalvariables = gllist.ToArray();
            }

            #endregion globalvariables

            #region GameModes

            if (game.Modes != null)
            {
                var list = new List<gameGameMode>();
                foreach (var m in game.Modes)
                {
                    var nm = new gameGameMode();
                    nm.name = m.Name;
                    nm.image = m.Image = (m.Image ?? "").Replace(rootPath, "");
                    nm.playerCount = m.PlayerCount;
                    nm.shortDescription = nm.shortDescription;

					list.Add(nm);
                }
                save.gameModes = list.ToArray();
            }

            #endregion GameModes

            var serializer = new XmlSerializer(typeof(game));
            directory = new FileInfo(game.InstallPath).Directory.FullName;
            using (var fs = File.Open(game.Filename, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                serializer.Serialize(fs, save);
            }
            return File.ReadAllBytes(game.Filename);
        }
Exemplo n.º 2
0
        public byte[] Serialize(object obj)
        {
            if ((obj is Game) == false)
            {
                throw new InvalidOperationException("obj must be typeof Game");
            }

            var game     = obj as Game;
            var rootPath = new DirectoryInfo(game.InstallPath).FullName;

            var save = new game();

            save.id                  = game.Id.ToString();
            save.name                = game.Name;
            save.card                = new gameCard();
            save.card.back           = game.CardBack;
            save.card.front          = game.CardFront;
            save.card.height         = game.CardHeight.ToString();
            save.card.width          = game.CardWidth.ToString();
            save.card.cornerRadius   = game.CardCornerRadius.ToString();
            save.version             = game.Version.ToString();
            save.authors             = string.Join(",", game.Authors);
            save.description         = game.Description;
            save.gameurl             = game.GameUrl;
            save.iconurl             = game.IconUrl;
            save.tags                = string.Join(" ", game.Tags);
            save.octgnVersion        = game.OctgnVersion.ToString();
            save.markersize          = game.MarkerSize;
            save.usetwosidedtable    = game.UseTwoSidedTable ? boolean.True : boolean.False;
            save.noteBackgroundColor = game.NoteBackgroundColor;
            save.noteForegroundColor = game.NoteForegroundColor;
            save.scriptVersion       = game.ScriptVersion == null ? null : game.ScriptVersion.ToString();

            #region Variables

            save.variables = (game.Variables ?? new List <Variable>()).Select(x => new gameVariable()
            {
                @default = x.Default.ToString(),
                global   = x.Global ? boolean.True : boolean.False,
                name     = x.Name,
                reset    = x.Reset ? boolean.True : boolean.False,
            }).ToArray();

            #endregion
            #region table
            save.table = SerializeGroup(game.Table, rootPath);
            #endregion table
            #region shared

            if (game.GlobalPlayer != null)
            {
                var gs    = new gameShared();
                var clist = new List <counter>();
                foreach (var counter in game.GlobalPlayer.Counters)
                {
                    var c = new counter();
                    c.name     = counter.Name;
                    c.icon     = (counter.Icon ?? "").Replace(rootPath, "");
                    c.reset    = counter.Reset ? boolean.True : boolean.False;
                    c.@default = counter.Start.ToString();
                    clist.Add(c);
                }
                var glist = new List <group>();
                foreach (var group in game.GlobalPlayer.Groups)
                {
                    glist.Add(SerializeGroup(group, rootPath));
                }
                gs.group    = glist.ToArray();
                gs.counter  = clist.ToArray();
                save.shared = gs;
            }
            #endregion shared
            #region player
            if (game.Player != null)
            {
                var player = new gamePlayer();
                var ilist  = new List <object>();
                foreach (var counter in game.Player.Counters)
                {
                    var c = new counter();
                    c.name     = counter.Name;
                    c.icon     = (counter.Icon ?? "").Replace(rootPath, "");
                    c.reset    = counter.Reset ? boolean.True : boolean.False;
                    c.@default = counter.Start.ToString();
                    ilist.Add(c);
                }
                foreach (var v in game.Player.GlobalVariables)
                {
                    var gv = new gamePlayerGlobalvariable();
                    gv.name  = v.Name;
                    gv.value = v.Value;
                    ilist.Add(gv);
                }
                ilist.Add(SerializeGroup(game.Player.Hand, rootPath));
                foreach (var g in game.Player.Groups)
                {
                    ilist.Add(SerializeGroup(g, rootPath));
                }
                player.Items   = ilist.ToArray();
                player.summary = game.Player.IndicatorsFormat;
                save.player    = player;
            }
            #endregion player
            #region documents

            if (game.Documents != null)
            {
                var docList = new List <gameDocument>();
                foreach (var d in game.Documents)
                {
                    var doc = new gameDocument();
                    doc.icon = (d.Icon ?? "").Replace(rootPath, "");
                    doc.name = d.Name;
                    doc.src  = (d.Source ?? "").Replace(rootPath, "");
                    docList.Add(doc);
                }
                save.documents = docList.ToArray();
            }

            #endregion documents
            #region sounds

            if (game.Sounds != null)
            {
                var soundList = new List <gameSound>();
                foreach (var d in game.Sounds)
                {
                    var doc = new gameSound();
                    doc.name = d.Value.Name;
                    doc.src  = (d.Value.Src ?? "").Replace(rootPath, "");
                    soundList.Add(doc);
                }
                save.sounds = soundList.ToArray();
            }

            #endregion sounds
            #region deck

            if (game.DeckSections != null)
            {
                var dl = new List <deckSection>();
                foreach (var s in game.DeckSections)
                {
                    var sec = new deckSection();
                    sec.name  = s.Value.Name;
                    sec.group = s.Value.Group;
                    dl.Add(sec);
                }
                save.deck = dl.ToArray();
            }

            if (game.SharedDeckSections != null)
            {
                var dl = new List <deckSection>();
                foreach (var s in game.SharedDeckSections)
                {
                    var sec = new deckSection();
                    sec.name  = s.Value.Name;
                    sec.group = s.Value.Group;
                    dl.Add(sec);
                }
                save.sharedDeck = dl.ToArray();
            }
            #endregion deck
            #region card

            if (game.CustomProperties != null)
            {
                var pl = new List <propertyDef>();
                foreach (var prop in game.CustomProperties)
                {
                    if (prop.Name == "Name")
                    {
                        continue;
                    }
                    var pd = new propertyDef();
                    pd.name       = prop.Name;
                    pd.type       = (propertyDefType)Enum.Parse(typeof(propertyDefType), prop.Type.ToString());
                    pd.hidden     = prop.Hidden.ToString();
                    pd.ignoreText = prop.IgnoreText ? boolean.True : boolean.False;
                    switch (prop.TextKind)
                    {
                    case PropertyTextKind.FreeText:
                        pd.textKind = propertyDefTextKind.Free;
                        break;

                    case PropertyTextKind.Enumeration:
                        pd.textKind = propertyDefTextKind.Enum;
                        break;

                    case PropertyTextKind.Tokens:
                        pd.textKind = propertyDefTextKind.Tokens;
                        break;
                    }
                    pl.Add(pd);
                }
                save.card.property = pl.ToArray();
            }
            #endregion card
            #region fonts

            if (game.Fonts != null)
            {
                var flist = new List <gameFont>();
                foreach (var font in game.Fonts)
                {
                    var f = new gameFont();
                    f.src    = (font.Src ?? "").Replace(rootPath, "");
                    f.size   = (uint)font.Size;
                    f.target = (fonttarget)Enum.Parse(typeof(fonttarget), font.Target);
                    flist.Add(f);
                }
                save.fonts = flist.ToArray();
            }
            #endregion fonts
            #region scripts

            if (game.Scripts != null)
            {
                var scriptList = new List <gameScript>();
                foreach (var script in game.Scripts)
                {
                    var f = new gameScript();
                    f.src = script;
                    scriptList.Add(f);
                }
                save.scripts = scriptList.ToArray();
            }

            #endregion scripts
            #region events

            if (game.Events != null)
            {
                var eventList = new List <gameEvent>();
                foreach (var e in game.Events.SelectMany(x => x.Value))
                {
                    var eve = new gameEvent();
                    eve.name   = e.Name;
                    eve.action = e.PythonFunction;
                    eventList.Add(eve);
                }
                save.events = eventList.ToArray();
            }

            #endregion events
            #region proxygen

            save.proxygen = new gameProxygen();
            save.proxygen.definitionsrc = game.ProxyGenSource;
            #endregion proxygen
            #region globalvariables

            if (game.GlobalVariables != null)
            {
                var gllist = new List <gameGlobalvariable>();
                foreach (var gv in game.GlobalVariables)
                {
                    var v = new gameGlobalvariable();
                    v.name  = gv.Name;
                    v.value = gv.Value;
                    gllist.Add(v);
                }
                save.globalvariables = gllist.ToArray();
            }

            #endregion globalvariables

            #region GameModes

            if (game.Modes != null)
            {
                var list = new List <gameGameMode>();
                foreach (var m in game.Modes)
                {
                    var nm = new gameGameMode();
                    nm.name             = m.Name;
                    nm.image            = m.Image = (m.Image ?? "").Replace(rootPath, "");
                    nm.playerCount      = m.PlayerCount;
                    nm.shortDescription = nm.shortDescription;

                    list.Add(nm);
                }
                save.gameModes = list.ToArray();
            }

            #endregion GameModes

            var serializer = new XmlSerializer(typeof(game));
            directory = new FileInfo(game.InstallPath).Directory.FullName;
            using (var fs = File.Open(game.Filename, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                serializer.Serialize(fs, save);
            }
            return(File.ReadAllBytes(game.Filename));
        }