public void Initialize(bool tryUpdateCoreJar = true, string coreJarRemoteBaseUrl = "http://www.mkhx.cc/resources/lib/corejar", string javaHome = null) { OnLogging(TraceLevel.Info, string.Format("Initializing GameLauncherFacade...tryUpdateCoreJar = {0}", tryUpdateCoreJar)); var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var jarPath = Path.Combine(assemblyDir, coreJarFileName); OnLogging(TraceLevel.Info, string.Format("Local CoreJarPath = {0}", jarPath)); bool jarExists = File.Exists(jarPath); OnLogging(TraceLevel.Info, string.Format("Local CoreJarExists = {0}", jarExists)); var verPath = Path.Combine(assemblyDir, coreJarVersionFileName); OnLogging(TraceLevel.Info, string.Format("Local CoreJarVersionFilePath = {0}", verPath)); var verExists = File.Exists(verPath); OnLogging(TraceLevel.Info, string.Format("Local CoreJarVersionFileExists = {0}", verExists)); long existingVer = 0; if (jarExists && verExists) { var existingVerText = File.ReadAllText(verPath); var parseSuccess = long.TryParse(existingVerText, out existingVer); if (parseSuccess) { OnLogging(TraceLevel.Info, string.Format("Successfully get local core jar version: {0}", existingVer)); } else { OnLogging(TraceLevel.Warning, string.Format("Failed to get local core jar version! Invalid version text: {0}", existingVerText)); } } if (tryUpdateCoreJar || !jarExists) { OnLogging(TraceLevel.Info, "Trying to update local core jar with latest one on server."); using (WebClient client = new WebClient()) { try { var remoteVerUrl = coreJarRemoteBaseUrl + "/" + coreJarVersionFileName; long latestVer = long.MaxValue; try { OnLogging(TraceLevel.Info, string.Format("Downloading version file from {0}...", remoteVerUrl)); var latestVerText = client.DownloadString(remoteVerUrl); OnLogging(TraceLevel.Info, "Done!"); if (!long.TryParse(latestVerText, out latestVer)) { OnLogging(TraceLevel.Warning, string.Format("Server returns an invalid core jar file version: {0}. Force update...", latestVerText)); } } catch (Exception e) { OnLogging(TraceLevel.Error, string.Format("Failed to get remote core jar version: {0}", e)); OnLogging(TraceLevel.Info, "Force update!"); } OnLogging(TraceLevel.Info, string.Format("Remote core jar version: {0}", latestVer)); if (!jarExists || existingVer < latestVer) { OnLogging(TraceLevel.Info, "Trying to replace local core jar with remote one..."); var remoteCoreJarUrl = coreJarRemoteBaseUrl + "/" + coreJarFileName; OnLogging(TraceLevel.Info, string.Format("Downloading core jar file from {0}...", remoteCoreJarUrl)); client.DownloadFile(remoteCoreJarUrl, jarPath); OnLogging(TraceLevel.Info, "Done!"); var remoteCoreJarVerFileUrl = coreJarRemoteBaseUrl + "/" + coreJarVersionFileName; OnLogging(TraceLevel.Info, string.Format("Downloading core jar version file from {0}...", remoteCoreJarVerFileUrl)); client.DownloadFile(remoteCoreJarVerFileUrl, verPath); OnLogging(TraceLevel.Info, "Done!"); } else { OnLogging(TraceLevel.Info, "Local core jar file is already update-to-date."); } } catch (Exception e) { OnLogging(TraceLevel.Error, "Failed to update local core jar with remote one due to error: " + e.ToString()); if (!jarExists) { throw new InvalidOperationException("Core jar file does not exists either on server or at local.", e); } } } } OnLogging(TraceLevel.Info, "Initializing JVM..."); var setup = new net.sf.jni4net.BridgeSetup(); if (!string.IsNullOrEmpty(javaHome)) { setup.JavaHome = javaHome; } setup.AddClassPath(jarPath); this.jvm = net.sf.jni4net.Bridge.CreateJVM(setup); this.facadeClass = this.jvm.FindClass("cfvbaibai/cardfantasy/game/launcher/GameLauncherFacade"); OnLogging(TraceLevel.Info, "JVM initialized!"); OnLogging(TraceLevel.Info, "Facade is fully initialized."); }
public void Initialize(bool tryUpdateCoreJar = true, string coreJarRemoteBaseUrl = "http://www.mkhx.cc/resources/lib/corejar", string javaHome = null) { OnLogging(TraceLevel.Info, string.Format("Initializing GameLauncherFacade...tryUpdateCoreJar = {0}", tryUpdateCoreJar)); var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var jarPath = Path.Combine(assemblyDir, coreJarFileName); OnLogging(TraceLevel.Info, string.Format("Local CoreJarPath = {0}", jarPath)); bool jarExists = File.Exists(jarPath); OnLogging(TraceLevel.Info, string.Format("Local CoreJarExists = {0}", jarExists)); var verPath = Path.Combine(assemblyDir, coreJarVersionFileName); OnLogging(TraceLevel.Info, string.Format("Local CoreJarVersionFilePath = {0}", verPath)); var verExists = File.Exists(verPath); OnLogging(TraceLevel.Info, string.Format("Local CoreJarVersionFileExists = {0}", verExists)); long existingVer = 0; if (jarExists && verExists) { var existingVerText = File.ReadAllText(verPath); var parseSuccess = long.TryParse(existingVerText, out existingVer); if (parseSuccess) { OnLogging(TraceLevel.Info, string.Format("Successfully get local core jar version: {0}", existingVer)); } else { OnLogging(TraceLevel.Warning, string.Format("Failed to get local core jar version! Invalid version text: {0}", existingVerText)); } } if (tryUpdateCoreJar || !jarExists) { OnLogging(TraceLevel.Info, "Trying to update local core jar with latest one on server."); using (WebClient client = new WebClient()) { try { var remoteVerUrl = coreJarRemoteBaseUrl + "/" + coreJarVersionFileName; long latestVer = long.MaxValue; try { OnLogging(TraceLevel.Info, string.Format("Downloading version file from {0}...", remoteVerUrl)); var latestVerText = client.DownloadString(remoteVerUrl); OnLogging(TraceLevel.Info, "Done!"); if (!long.TryParse(latestVerText, out latestVer)) { OnLogging(TraceLevel.Warning, string.Format("Server returns an invalid core jar file version: {0}. Force update...", latestVerText)); } } catch (Exception e) { OnLogging(TraceLevel.Error, string.Format("Failed to get remote core jar version: {0}", e)); OnLogging(TraceLevel.Info, "Force update!"); } OnLogging(TraceLevel.Info, string.Format("Remote core jar version: {0}", latestVer)); if (!jarExists || existingVer < latestVer) { OnLogging(TraceLevel.Info, "Trying to replace local core jar with remote one..."); var remoteCoreJarUrl = coreJarRemoteBaseUrl + "/" + coreJarFileName; OnLogging(TraceLevel.Info, string.Format("Downloading core jar file from {0}...", remoteCoreJarUrl)); client.DownloadFile(remoteCoreJarUrl, jarPath); OnLogging(TraceLevel.Info, "Done!"); var remoteCoreJarVerFileUrl = coreJarRemoteBaseUrl + "/" + coreJarVersionFileName; OnLogging(TraceLevel.Info, string.Format("Downloading core jar version file from {0}...", remoteCoreJarVerFileUrl)); client.DownloadFile(remoteCoreJarVerFileUrl, verPath); OnLogging(TraceLevel.Info, "Done!"); } else { OnLogging(TraceLevel.Info, "Local core jar file is already update-to-date."); } } catch (Exception e) { OnLogging(TraceLevel.Error, "Failed to update local core jar with remote one due to error: " + e.ToString()); if (!jarExists) { throw new InvalidOperationException("Core jar file does not exists either on server or at local.", e); } } } } OnLogging(TraceLevel.Info, "Initializing JVM..."); var setup = new net.sf.jni4net.BridgeSetup(); if (!string.IsNullOrEmpty(javaHome)) { setup.JavaHome = javaHome; } setup.AddClassPath(jarPath); this.jvm = net.sf.jni4net.Bridge.CreateJVM(setup); this.facadeClass = this.jvm.FindClass("cfvbaibai/cardfantasy/game/launcher/GameLauncherFacade"); OnLogging(TraceLevel.Info, "JVM initialized!"); OnLogging(TraceLevel.Info, "Facade is fully initialized."); }