예제 #1
0
        /// <summary>
        /// Save the mod settings. Saves the settings to {UserIO.GetSavePath("Saves")}/modsettings-{Metadata.Name}.yaml by default.
        /// </summary>
        public virtual void SaveSettings()
        {
            if (SettingsType == null || _Settings == null)
            {
                return;
            }

            string path = patch_UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            Directory.CreateDirectory(Path.GetDirectoryName(path));

            try {
                using (Stream stream = File.OpenWrite(path)) {
                    if (_Settings is EverestModuleBinarySettings)
                    {
                        using (BinaryWriter writer = new BinaryWriter(stream))
                            ((EverestModuleBinarySettings)_Settings).Write(writer);
                    }
                    else
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                            YamlHelper.Serializer.Serialize(writer, _Settings, SettingsType);
                    }
                }
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to save the settings of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }
예제 #2
0
        /// <summary>
        /// Serialize the mod session into its raw bytes, to be fed into WriteSession immediately or async.
        /// </summary>
        public virtual byte[] SerializeSession(int index)
        {
            if (!SaveDataAsync && !ForceSaveDataAsync)
            {
                throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
            }

            if (SessionType == null)
            {
                return(null);
            }

            try {
                using (MemoryStream stream = new MemoryStream()) {
                    if (_Session is EverestModuleBinarySession bs)
                    {
                        using (BinaryWriter writer = new BinaryWriter(new UndisposableStream(stream)))
                            bs.Write(writer);
                    }
                    else
                    {
                        using (StreamWriter writer = new StreamWriter(new UndisposableStream(stream)))
                            YamlHelper.Serializer.Serialize(writer, _Session, SessionType);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    return(stream.ToArray());
                }
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to serialize the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
                return(null);
            }
        }
예제 #3
0
        /// <summary>
        /// Read the mod session bytes from a file, {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual byte[] ReadSession(int index)
        {
            if (!SaveDataAsync && !ForceSaveDataAsync)
            {
                throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
            }

            if (SessionType == null)
            {
                return(null);
            }

            string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsession-" + Metadata.Name);

            if (!File.Exists(path))
            {
                return(null);
            }

            try {
                return(File.ReadAllBytes(path));
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to read the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
                return(null);
            }
        }
예제 #4
0
        /// <summary>
        /// Save the mod settings. Saves the settings to {UserIO.GetSavePath("Saves")}/modsettings-{Metadata.Name}.yaml by default.
        /// </summary>
        public virtual void SaveSettings()
        {
            bool forceFlush = ForceSaveDataFlush > 0;

            if (forceFlush)
            {
                ForceSaveDataFlush--;
            }

            if (SettingsType == null || _Settings == null)
            {
                return;
            }

            string path = patch_UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            Directory.CreateDirectory(Path.GetDirectoryName(path));

            try {
                using (FileStream stream = File.OpenWrite(path)) {
                    if (_Settings is EverestModuleBinarySettings)
                    {
                        using (BinaryWriter writer = new BinaryWriter(stream)) {
                            ((EverestModuleBinarySettings)_Settings).Write(writer);
                            if (forceFlush || ((CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
                            {
                                stream.Flush(true);
                            }
                        }
                    }
                    else
                    {
                        using (StreamWriter writer = new StreamWriter(stream)) {
                            YamlHelper.Serializer.Serialize(writer, _Settings, SettingsType);
                            if (forceFlush || ((CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
                            {
                                stream.Flush(true);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to save the settings of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }
예제 #5
0
        internal static void Initialize(Game game)
        {
            if (Initialized)
            {
                return;
            }
            Initialized = true;

            Type      t_TextInputExt = typeof(Keyboard).Assembly.GetType("Microsoft.Xna.Framework.Input.TextInputEXT");
            EventInfo e_TextInput    = t_TextInputExt?.GetEvent("TextInput");

            if (e_TextInput != null)
            {
                // FNA offers Microsoft.Xna.Framework.Input.TextInputEXT,
                // public static event Action<char> TextInput;
                e_TextInput.AddEventHandler(null, new Action <char>(ReceiveTextInput).CastDelegate(e_TextInput.EventHandlerType));

                // SDL2 offers SDL_GetClipboardText and SDL_SetClipboardText
                Type t_SDL2 = typeof(Keyboard).Assembly.GetType("SDL2.SDL");
                _GetClipboardText = t_SDL2.GetMethod("SDL_GetClipboardText").CreateDelegate(typeof(Func <string>)) as Func <string>;
                Func <string, int> setClipboardText = t_SDL2.GetMethod("SDL_SetClipboardText").CreateDelegate(typeof(Func <string, int>)) as Func <string, int>;
                _SetClipboardText = value => setClipboardText(value);
            }
            else
            {
                // XNA requires WinForms message hooks.
                FormsHook = new XNAFormsHook(game.Window.Handle, (ref Message msg) => {
                    if (msg.Msg != 0x0102)
                    {
                        return;
                    }
                    ReceiveTextInput((char)msg.WParam);
                });

                // WinForms offers Clipboard.GetText and SetText
                Type          t_Clipboard      = Assembly.Load("System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").GetType("System.Windows.Forms.Clipboard");
                Func <string> getClipboardText = t_Clipboard.GetMethod("GetText", new Type[] { }).CreateDelegate(typeof(Func <string>)) as Func <string>;
                _GetClipboardText = () => STAThreadHelper.Get(getClipboardText).GetResult();
                Action <string> setClipboardText = t_Clipboard.GetMethod("SetText", new Type[] { typeof(string) }).CreateDelegate(typeof(Action <string>)) as Action <string>;
                _SetClipboardText = (value) => STAThreadHelper.Get(() => {
                    try {
                        setClipboardText(string.IsNullOrEmpty(value) ? "\0" : value);
                    } catch (ExternalException e) {
                        Logger.Log(LogLevel.Warn, "TextInputs", "Failed to set the clipboard");
                        Logger.LogDetailed(e);
                    }
                    return(value);
                }).GetResult();
            }
        }
예제 #6
0
        /// <summary>
        /// Write the mod session bytes into a file, {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual void WriteSession(int index, byte[] data)
        {
            bool forceFlush = ForceSaveDataFlush > 0;

            if (forceFlush)
            {
                ForceSaveDataFlush--;
            }

            if (!SaveDataAsync && !ForceSaveDataAsync)
            {
                throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
            }

            if (SessionType == null)
            {
                return;
            }

            string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsession-" + Metadata.Name);

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            if (data == null)
            {
                return;
            }

            Directory.CreateDirectory(Path.GetDirectoryName(path));

            try {
                using (FileStream stream = File.OpenWrite(path)) {
                    stream.Write(data, 0, data.Length);
                    if (forceFlush || (SaveDataAsync && (CoreModule.Settings.SaveDataFlush ?? true) && !MainThreadHelper.IsMainThread))
                    {
                        stream.Flush(true);
                    }
                }
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to write the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }
예제 #7
0
        /// <summary>
        /// Load the mod settings. Loads the settings from {UserIO.GetSavePath("Saves")}/modsettings-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual void LoadSettings()
        {
            if (SettingsType == null)
            {
                return;
            }

            _Settings = (EverestModuleSettings)SettingsType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);

            string path = patch_UserIO.GetSaveFilePath("modsettings-" + Metadata.Name);

            // Temporary fallback to help migrate settings from their old location.
            if (!File.Exists(path))
            {
                path = Path.Combine(Everest.PathEverest, "ModSettings-OBSOLETE", Metadata.Name + ".yaml");
            }

            if (!File.Exists(path))
            {
                return;
            }

            try {
                using (Stream stream = File.OpenRead(path)) {
                    if (_Settings is EverestModuleBinarySettings)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            ((EverestModuleBinarySettings)_Settings).Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_Settings).Deserialize(reader, SettingsType);
                    }
                }
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to load the settings of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }

            if (_Settings == null)
            {
                _Settings = (EverestModuleSettings)SettingsType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            }
        }
예제 #8
0
 public Task <Source> Request()
 {
     if (_RequestTask != null)
     {
         return(_RequestTask);
     }
     _RequestTask = new Task <Source>(() => {
         try {
             return(_RequestStart());
         } catch (Exception e) {
             ErrorDialog = "updater_versions_err_download";
             Logger.Log(LogLevel.Warn, "updater", "Uncaught exception while loading Everest version list");
             Logger.LogDetailed(e);
             return(this);
         }
     });
     _RequestTask.Start();
     return(_RequestTask);
 }
예제 #9
0
        /// <summary>
        /// Load the mod session. Loads the session from {UserIO.GetSavePath("Saves")}/{SaveData.GetFilename(index)}-modsession-{Metadata.Name}.celeste by default.
        /// </summary>
        public virtual void LoadSession(int index, bool forceNew)
        {
            if (SessionType == null)
            {
                return;
            }

            _Session       = (EverestModuleSession)SessionType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            _Session.Index = index;

            if (forceNew)
            {
                return;
            }

            string path = patch_UserIO.GetSaveFilePath(patch_SaveData.GetFilename(index) + "-modsession-" + Metadata.Name);

            if (!File.Exists(path))
            {
                return;
            }

            try {
                using (Stream stream = File.OpenRead(path)) {
                    if (_Session is EverestModuleBinarySession)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            ((EverestModuleBinarySession)_Session).Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_Session).Deserialize(reader, SessionType);
                    }
                }
                _Session.Index = index;
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to load the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }
예제 #10
0
        /// <summary>
        /// Deserialize the mod session from its raw bytes, fed with data from ReadSession either immediately or async.
        /// </summary>
        public virtual void DeserializeSession(int index, byte[] data)
        {
            if (!SaveDataAsync && !ForceSaveDataAsync)
            {
                throw new Exception($"{Metadata.Name} overrides old methods or otherwise disabled async save data support.");
            }

            if (SessionType == null)
            {
                return;
            }

            _Session       = (EverestModuleSession)SessionType.GetConstructor(Everest._EmptyTypeArray).Invoke(Everest._EmptyObjectArray);
            _Session.Index = index;

            if (data == null)
            {
                return;
            }

            try {
                using (MemoryStream stream = new MemoryStream(data)) {
                    if (_Session is EverestModuleBinarySession bs)
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                            bs.Read(reader);
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                            YamlHelper.DeserializerUsing(_Session).Deserialize(reader, SessionType);
                    }
                }
                _Session.Index = index;
            } catch (Exception e) {
                Logger.Log(LogLevel.Warn, "EverestModule", $"Failed to deserialize the session of {Metadata.Name}!");
                Logger.LogDetailed(e);
            }
        }