Esempio n. 1
0
        private async static void OnMyAfterSelect(SINner sinner, CharacterCache objCache, TreeViewEventArgs treeViewEventArgs)
        {
            string loadFilePath = null;

            using (new CursorWait(true, PluginHandler.MainForm))
            {
                string zipPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "SINner", sinner.Id.Value.ToString());
                if (Directory.Exists(zipPath))
                {
                    var files = Directory.EnumerateFiles(zipPath, "*.chum5", SearchOption.TopDirectoryOnly);
                    foreach (var file in files)
                    {
                        DateTime lastwrite = File.GetLastWriteTime(file);
                        if ((lastwrite >= sinner.LastChange) ||
                            sinner.LastChange == null)
                        {
                            loadFilePath      = file;
                            objCache.FilePath = loadFilePath;
                            break;
                        }
                        File.Delete(file);
                    }
                }
                if (String.IsNullOrEmpty(objCache.FilePath))
                {
                    objCache.FilePath = await DownloadFileTask(sinner, objCache);
                }

                if (!String.IsNullOrEmpty(objCache.FilePath))
                {
                    //I copy the values, because I dont know what callbacks are registered...
                    var tempCache = new CharacterCache(objCache.FilePath);
                    objCache.Background     = tempCache.Background;
                    objCache.MugshotBase64  = tempCache.MugshotBase64;
                    objCache.BuildMethod    = tempCache.BuildMethod;
                    objCache.CharacterAlias = tempCache.CharacterAlias;
                    objCache.CharacterName  = tempCache.CharacterName;
                    objCache.CharacterNotes = tempCache.CharacterNotes;
                    objCache.Concept        = tempCache.Concept;
                    objCache.Created        = tempCache.Created;
                    objCache.Description    = tempCache.Description;
                    objCache.Essence        = tempCache.Essence;
                    objCache.GameNotes      = tempCache.GameNotes;
                    objCache.Karma          = tempCache.Karma;
                    objCache.FileName       = tempCache.FileName;
                    objCache.Metatype       = tempCache.Metatype;
                    objCache.Metavariant    = tempCache.Metavariant;
                    objCache.PlayerName     = tempCache.PlayerName;
                    objCache.SettingsFile   = tempCache.SettingsFile;
                }
                treeViewEventArgs.Node.Text = objCache.CalculatedName();
            }
        }
Esempio n. 2
0
        private static TreeNode GetCharacterRosterTreeNodeRecursive(SINnerList list, ref ConcurrentDictionary <string, CharacterCache> CharCache)
        {
            var sinner = list.SiNner;

            if (String.IsNullOrEmpty(sinner?.JsonSummary))
            {
                return(null);
            }
            ;
            CharacterCache objCache = Newtonsoft.Json.JsonConvert.DeserializeObject <CharacterCache>(sinner.JsonSummary);

            SetEventHandlers(sinner, objCache);
            TreeNode objListNode = new TreeNode
            {
                Text             = objCache.CalculatedName(),
                Tag              = sinner.Id.Value.ToString(),
                ToolTipText      = "Last Change: " + sinner.LastChange,
                ContextMenuStrip = PluginHandler.MainForm.CharacterRoster.ContextMenuStrip
            };

            if (!string.IsNullOrEmpty(objCache.ErrorText))
            {
                objListNode.ForeColor    = Color.Red;
                objListNode.ToolTipText += Environment.NewLine + Environment.NewLine + LanguageManager.GetString("String_Error", GlobalOptions.Language)
                                           + LanguageManager.GetString("String_Colon", GlobalOptions.Language) + Environment.NewLine + objCache.ErrorText;
            }
            CharacterCache delObj;

            CharCache.TryRemove(sinner.Id.Value.ToString(), out delObj);
            CharCache.TryAdd(sinner.Id.Value.ToString(), objCache);
            foreach (var childlist in list.SinList)
            {
                var childnode = GetCharacterRosterTreeNodeRecursive(childlist, ref CharCache);
                if (childnode != null)
                {
                    objListNode.Nodes.Add(childnode);
                }
            }
            return(objListNode);
        }
Esempio n. 3
0
        /// <summary>
        /// Generates a character cache, which prevents us from repeatedly loading XmlNodes or caching a full character.
        /// </summary>
        /// <param name="strFile"></param>
        internal async static Task <IEnumerable <TreeNode> > GetCharacterRosterTreeNode(ConcurrentDictionary <string, CharacterCache> CharCache, bool forceUpdate)
        {
            if ((MyTreeNodeList != null) && !forceUpdate)
            {
                return(MyTreeNodeList);
            }
            try
            {
                var response = await StaticUtils.Client.GetSINnersByAuthorizationWithHttpMessagesAsync();

                if (MyOnlineTreeNode == null)
                {
                    MyOnlineTreeNode = new TreeNode()
                    {
                        Text = "Online",
                        Tag  = "OnlineTag"
                    };
                }
                else
                {
                    PluginHandler.MainForm.DoThreadSafe(() =>
                    {
                        MyOnlineTreeNode.Nodes.Clear();
                    });
                }
                if (response.Response.StatusCode == HttpStatusCode.BadRequest)
                {
                    string msg     = "Could not load online Sinners: " + response.Response.ReasonPhrase;
                    var    content = await response.Response.Content.ReadAsStringAsync();

                    msg += Environment.NewLine + "Content: " + content;
                    System.Diagnostics.Trace.TraceWarning(msg);
                    MyOnlineTreeNode.ToolTipText = msg;
                    return(new List <TreeNode>()
                    {
                        MyOnlineTreeNode
                    });
                }
                if (response == null || response.Body == null || response.Body?.SinLists == null)
                {
                    return(new List <TreeNode>()
                    {
                        MyOnlineTreeNode
                    });
                }
                foreach (var list in response.Body.SinLists)
                {
                    try
                    {
                        if (!list.SiNners.Any())
                        {
                            continue;
                        }
                        TreeNode objListNode = new TreeNode
                        {
                            Text             = list.Header,
                            Tag              = list,
                            ContextMenuStrip = PluginHandler.MainForm.CharacterRoster.ContextMenuStrip
                        };
                        foreach (var sinner in list.SiNners)
                        {
                            if (String.IsNullOrEmpty(sinner.JsonSummary))
                            {
                                continue;
                            }
                            CharacterCache objCache = Newtonsoft.Json.JsonConvert.DeserializeObject <CharacterCache>(sinner.JsonSummary);
                            SetEventHandlers(sinner, objCache);

                            TreeNode objNode = new TreeNode
                            {
                                Text             = objCache.CalculatedName(),
                                Tag              = sinner.Id.Value.ToString(),
                                ToolTipText      = "Last Change: " + sinner.LastChange,
                                ContextMenuStrip = PluginHandler.MainForm.CharacterRoster.ContextMenuStrip
                            };
                            if (!string.IsNullOrEmpty(objCache.ErrorText))
                            {
                                objNode.ForeColor    = Color.Red;
                                objNode.ToolTipText += Environment.NewLine + Environment.NewLine + LanguageManager.GetString("String_Error", GlobalOptions.Language)
                                                       + LanguageManager.GetString("String_Colon", GlobalOptions.Language) + Environment.NewLine + objCache.ErrorText;
                            }
                            CharacterCache delObj;
                            CharCache.TryRemove(sinner.Id.Value.ToString(), out delObj);
                            CharCache.TryAdd(sinner.Id.Value.ToString(), objCache);
                            PluginHandler.MainForm.DoThreadSafe(() =>
                            {
                                objListNode.Nodes.Add(objNode);
                            });
                        }

                        PluginHandler.MainForm.DoThreadSafe(() =>
                        {
                            MyOnlineTreeNode.Nodes.Add(objListNode);
                        });
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Trace.TraceWarning("Could not deserialize CharacterCache-Object: " + list.Header);
                    }
                }
                if (MyOnlineTreeNode.Nodes.Count > 0)
                {
                    return MyTreeNodeList = new List <TreeNode>()
                    {
                               MyOnlineTreeNode
                    }
                }
                ;
                else
                {
                    return(MyTreeNodeList = null);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ex.ToString());
                throw;
            }
        }
Esempio n. 4
0
        private static void SetEventHandlers(SINners.Models.SINner sinner, CharacterCache objCache)
        {
            objCache.OnMyDoubleClick -= objCache.OnDefaultDoubleClick;
            objCache.OnMyDoubleClick += (sender, e) =>
            {
                var t = DownloadFileTask(sinner, objCache);
                t.ContinueWith((downloadtask) =>
                {
                    PluginHandler.MySINnerLoading = sinner;
                    PluginHandler.MainForm.CharacterRoster.SetMyEventHandlers(true);
                    string filepath = downloadtask.Result as string;
                    PluginHandler.MainForm.DoThreadSafe(() =>
                    {
                        Character c = PluginHandler.MainForm.LoadCharacter(filepath);
                        if (c != null)
                        {
                            SwitchToCharacter(c);
                        }
                        SwitchToCharacter(objCache);

                        PluginHandler.MainForm.CharacterRoster.SetMyEventHandlers(false);
                        PluginHandler.MySINnerLoading = null;
                    });
                });
            };
            objCache.OnMyAfterSelect -= objCache.OnDefaultAfterSelect;
            objCache.OnMyAfterSelect += (sender, treeViewEventArgs) =>
            {
                using (new CursorWait(true, PluginHandler.MainForm))
                {
                    if (String.IsNullOrEmpty(objCache.MugshotBase64))
                    {
                        var jsonString = DownloadSINnerExtendedTask(sinner, objCache).Result;
                    }

                    objCache.FilePath           = DownloadFileTask(sinner, objCache).Result;
                    treeViewEventArgs.Node.Text = objCache.CalculatedName();
                }
            };
            objCache.OnMyKeyDown -= objCache.OnDefaultKeyDown;
            objCache.OnMyKeyDown += async(sender, args) =>
            {
                try
                {
                    using (new CursorWait(true, PluginHandler.MainForm))
                    {
                        if (args.Item1.KeyCode == Keys.Delete)
                        {
                            var client = await StaticUtils.GetClient();

                            client.Delete(sinner.Id.Value);
                            objCache.ErrorText = "deleted!";
                            PluginHandler.MainForm.DoThreadSafe(() =>
                            {
                                PluginHandler.MainForm.CharacterRoster.LoadCharacters(false, false, false, true);
                            });
                        }
                    }
                }
                catch (HttpOperationException e)
                {
                    objCache.ErrorText  = e.Message;
                    objCache.ErrorText += Environment.NewLine + e.Response.Content;
                    System.Diagnostics.Trace.TraceWarning(e.ToString());
                }
                catch (Exception e)
                {
                    objCache.ErrorText = e.Message;
                    System.Diagnostics.Trace.TraceWarning(e.ToString());
                }
            };
        }