/// <summary> /// Load session /// </summary> /// <param name="path">Path</param> /// <returns>Session if successful, otherwise "null"</returns> public static Session Load(string path) { Session ret = null; try { if (path != null) { if (File.Exists(path)) { using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Read)) { ZipArchiveEntry entry = archive.GetEntry("meta.json"); if (entry != null) { using (Stream stream = entry.Open()) { SessionDataContract session_data = serializer.ReadObject(stream) as SessionDataContract; if (session_data != null) { ret = new Session(path, session_data); } } } } } } } catch (Exception e) { Console.Error.WriteLine(e); } return(ret); }
/// <summary> /// Launch SA:MP /// </summary> /// <param name="server">Server</param> /// <param name="username">Username</param> /// <param name="serverPassword">Server password</param> /// <param name="rconPassword">RCON password</param> /// <param name="debug">Debug mode</param> /// <param name="quitWhenDone">Quit when done</param> /// <param name="createSessionLog">Create session log</param> /// <param name="plugins">Plugins</param> /// <param name="f">Form to close</param> public static void LaunchSAMP(Server server, string username, string serverPassword, string rconPassword, bool debug, bool quitWhenDone, bool createSessionLog, PluginDataContract[] plugins, Form f) { if ((server != null) || debug) { if (debug || server.IsValid) { IntPtr mh = Kernel32.GetModuleHandle("kernel32.dll"); if (mh != IntPtr.Zero) { IntPtr load_library_w = Kernel32.GetProcAddress(mh, "LoadLibraryW"); if (load_library_w != IntPtr.Zero) { Kernel32.PROCESS_INFORMATION process_info; Kernel32.STARTUPINFO startup_info = new Kernel32.STARTUPINFO(); if (CheckIfGTASanAndreasIsLaunchable) { string modified_username = ((username == null) ? "" : username.Trim().Replace(' ', '_')); if (createSessionLog) { lastMediaState = SessionProvider.GetCurrentMediaState(); lastSessionData = new SessionDataContract(DateTime.Now, TimeSpan.Zero, SAMPProvider.CurrentVersion.Name, modified_username, (server == null) ? "" : server.IPPortString, (server == null) ? "" : server.Hostname, (server == null) ? "" : server.Gamemode, (server == null) ? "" : server.Language); } if (Kernel32.CreateProcess(GTASAExe, debug ? "-d" : "-c " + ((rconPassword == null) ? "" : rconPassword) + " -h " + server.IPv4AddressString + " -p " + server.Port + " -n " + modified_username + ((serverPassword == null) ? "" : (" -z " + serverPassword)), IntPtr.Zero, IntPtr.Zero, false, /* DETACHED_PROCESS */ 0x8 | /* CREATE_SUSPENDED */ 0x4, IntPtr.Zero, ExeDir, ref startup_info, out process_info)) { InjectPlugin(SAMPDLLPath, process_info.hProcess, load_library_w); PluginDataContract[] load_plugins = ((plugins == null) ? PluginsDataIO : plugins); foreach (PluginDataContract plugin in load_plugins) { if (plugin != null) { if (plugin.Enabled) { InstalledPlugin installed_plugin = PluginProvider.Update(plugin); if (installed_plugin != null) { InjectPlugin(installed_plugin.Path, process_info.hProcess, load_library_w); } } } } Kernel32.ResumeThread(process_info.hThread); Kernel32.CloseHandle(process_info.hProcess); if ((f != null)) { if (quitWhenDone && (!createSessionLog)) { f.Close(); } f.WindowState = FormWindowState.Minimized; } } } } } } } }
/// <summary> /// Create session /// </summary> /// <param name="path">Path</param> /// <param name="dateTime">Date and time</param> /// <param name="timeSpan">Time span</param> /// <param name="gameVersion">Game version</param> /// <param name="username">Username</param> /// <param name="ipPort">IP and port</param> /// <param name="hostname">Hostname</param> /// <param name="mode">Mode</param> /// <param name="language">Language</param> /// <param name="screenshotPaths">Screenshot paths</param> /// <param name="chatlogPath">Chatlog path</param> /// <param name="savedPositionsPath">Saved positions path</param> /// <returns>New session</returns> public static Session Create(string path, DateTime dateTime, TimeSpan timeSpan, string gameVersion, string username, string ipPort, string hostname, string mode, string language, string[] screenshotPaths, string chatlogPath, string savedPositionsPath) { Session ret = null; if (path != null) { if (path != null) { SessionDataContract session_data = new SessionDataContract(dateTime, timeSpan, gameVersion, username, ipPort, hostname, mode, language); try { if (File.Exists(path)) { File.Delete(path); } using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Create)) { ZipArchiveEntry entry = archive.CreateEntry("meta.json"); if (entry != null) { using (Stream stream = entry.Open()) { serializer.WriteObject(stream, session_data); } } if (screenshotPaths != null) { foreach (string screenshot_path in screenshotPaths) { if (screenshot_path != null) { if (File.Exists(screenshot_path)) { archive.CreateEntryFromFile(screenshot_path, "screenshots/" + System.IO.Path.GetFileName(screenshot_path)); } } } } if (chatlogPath != null) { if (File.Exists(chatlogPath)) { archive.CreateEntryFromFile(chatlogPath, "chatlog.txt"); } } if (savedPositionsPath != null) { if (File.Exists(savedPositionsPath)) { archive.CreateEntryFromFile(savedPositionsPath, "saved-positions.txt"); } } } ret = new Session(path, session_data); } catch (Exception e) { Console.Error.WriteLine(e); } } } return(ret); }
/// <summary> /// Constructor /// </summary> /// <param name="path">Path</param> /// <param name="sessionData">Session data</param> private Session(string path, SessionDataContract sessionData) { this.path = path; this.sessionData = sessionData; }