wxThread::ExitCode LWJGLInstallTask::TaskStart() { SetStatus(_("Installing new LWJGL...")); wxFFileInputStream in(m_path); wxZipInputStream zipIn(in); // make sure the directories are there wxString lwjgl_base = Path::Combine(settings->GetLwjglDir(), m_version); wxString nativesPath = Path::Combine( lwjgl_base, "natives"); bool success = wxDir::Make(nativesPath,wxS_DIR_DEFAULT,wxPATH_MKDIR_FULL); if(!success) return (wxThread::ExitCode) 0; std::auto_ptr<wxZipEntry> entry; while (entry.reset(zipIn.GetNextEntry()), entry.get() != NULL) { if (entry->IsDir()) continue; const wxString jarNames[] = { "jinput.jar", "lwjgl_util.jar", "lwjgl.jar" }; wxString name = entry->GetName(); wxString destFileName; // Put things in their places. for (int i = 0; i < 3; i++) if (name.EndsWith(jarNames[i])) destFileName = Path::Combine(lwjgl_base, jarNames[i]); // If we haven't found a jar file, look for natives. if (destFileName.IsEmpty()) { #if WINDOWS wxString nativesDir = "windows"; #elif OSX wxString nativesDir = "macosx"; #elif LINUX wxString nativesDir = "linux"; #endif if (name.Contains(nativesDir)) { destFileName = Path::Combine(nativesPath, name.AfterLast('/').AfterLast('\\')); } } // Now if destFileName is still empty, go to the next file. if (!destFileName.IsEmpty()) { SetStatus(_("Installing new LWJGL - Extracting " + name)); wxFFileOutputStream out(destFileName); zipIn.Read(out); } } return (ExitCode)1; }
wxString GetMinecraftJarVersion(wxFileName jar) { wxString fullpath = jar.GetFullPath(); wxString version = MCVer_Unknown; if(!jar.FileExists()) return version; std::auto_ptr<wxZipEntry> entry; // convert the local name we are looking for into the internal format wxString name = wxZipEntry::GetInternalName("net/minecraft/client/Minecraft.class",wxPATH_UNIX); // open the zip wxFFileInputStream inStream(jar.GetFullPath()); wxZipInputStream zipIn(inStream); // call GetNextEntry() until the required internal name is found do { entry.reset(zipIn.GetNextEntry()); } while (entry.get() != NULL && entry->GetInternalName() != name); auto myentry = entry.get(); if (myentry == NULL) return version; // we got the entry, read the data std::size_t size = myentry->GetSize(); char *classdata = new char[size]; zipIn.Read(classdata,size); try { char * temp = classdata; java::classfile Minecraft_jar(temp,size); auto cnst = Minecraft_jar.constants; auto iter = cnst.begin(); while (iter != cnst.end()) { const java::constant & constant = *iter; if(constant.type != java::constant::j_string_data) { iter++; continue; } auto & str = constant.str_data; const char * lookfor = "Minecraft Minecraft "; // length = 20 if(str.compare(0,20,lookfor) == 0) { version = str.substr(20).data(); break; } iter++; } } catch(java::classfile_exception &){} delete[] classdata; return version; }
wxThread::ExitCode ModderTask::TaskStart() { // Get the mod list ModList *modList = m_inst->GetModList(); wxFileName mcBin = m_inst->GetBinDir(); wxFileName mcJar = m_inst->GetMCJar(); wxFileName mcBackup = m_inst->GetMCBackup(); // Nothing to do if there are no jar mods to install, no backup and just the mc jar if(mcJar.FileExists() && !mcBackup.FileExists() && modList->empty()) { m_inst->SetNeedsRebuild(false); return (ExitCode)1; } SetStatus(_("Installing mods - backing up minecraft.jar...")); if (!mcBackup.FileExists() && !wxCopyFile(mcJar.GetFullPath(), mcBackup.GetFullPath())) { OnFail(_("Failed to back up minecraft.jar")); return (ExitCode)0; } if (mcJar.FileExists() && !wxRemoveFile(mcJar.GetFullPath())) { OnFail(_("Failed to delete old minecraft.jar")); return (ExitCode)0; } // TODO: good spot for user cancel check? or not... TaskStep(); // STEP 1 SetStatus(_("Installing mods - Opening minecraft.jar")); wxFFileOutputStream jarStream(mcJar.GetFullPath()); wxZipOutputStream zipOut(jarStream); // Files already added to the jar. // These files will be skipped. std::set<wxString> addedFiles; // Modify the jar TaskStep(); // STEP 2 SetStatus(_("Installing mods - Adding mod files...")); for (ModList::const_reverse_iterator iter = modList->rbegin(); iter != modList->rend(); iter++) { wxFileName modFileName = iter->GetFileName(); SetStatus(_("Installing mods - Adding ") + modFileName.GetFullName()); if (iter->IsZipMod()) { wxFFileInputStream modStream(modFileName.GetFullPath()); wxZipInputStream zipStream(modStream); std::auto_ptr<wxZipEntry> entry; while (entry.reset(zipStream.GetNextEntry()), entry.get() != NULL) { if (entry->IsDir()) continue; wxString name = entry->GetName(); if (addedFiles.count(name) == 0) { if (!zipOut.CopyEntry(entry.release(), zipStream)) break; addedFiles.insert(name); } } } else { wxFileName destFileName = modFileName; destFileName.MakeRelativeTo(m_inst->GetInstModsDir().GetFullPath()); wxString destFile = destFileName.GetFullPath(); if (addedFiles.count(destFile) == 0) { wxFFileInputStream input(modFileName.GetFullPath()); zipOut.PutNextEntry(destFile); zipOut.Write(input); addedFiles.insert(destFile); } } } { wxFFileInputStream inStream(mcBackup.GetFullPath()); wxZipInputStream zipIn(inStream); std::auto_ptr<wxZipEntry> entry; while (entry.reset(zipIn.GetNextEntry()), entry.get() != NULL) { wxString name = entry->GetName(); if (!name.Matches("META-INF*") && addedFiles.count(name) == 0) { if (!zipOut.CopyEntry(entry.release(), zipIn)) break; addedFiles.insert(name); } } } // Recompress the jar TaskStep(); // STEP 3 SetStatus(_("Installing mods - Recompressing jar...")); m_inst->SetNeedsRebuild(false); m_inst->UpdateVersion(true); return (ExitCode)1; }
void ModderTask::TaskStart() { // Get the mod list const ModList *modList = m_inst->GetModList(); wxFileName mcBin = m_inst->GetBinDir(); wxFileName mcJar = m_inst->GetMCJar(); wxFileName mcBackup = m_inst->GetMCBackup(); SetStatus(_("Installing mods - backing up minecraft.jar...")); if (!mcBackup.FileExists() && !wxCopyFile(mcJar.GetFullPath(), mcBackup.GetFullPath())) { OnFail(_("Failed to back up minecraft.jar")); return; } if (mcJar.FileExists() && !wxRemoveFile(mcJar.GetFullPath())) { OnFail(_("Failed to delete old minecraft.jar")); return; } if (TestDestroy()) return; TaskStep(); // STEP 1 SetStatus(_("Installing mods - Opening minecraft.jar")); wxFFileOutputStream jarStream(mcJar.GetFullPath()); wxZipOutputStream zipOut(jarStream); { wxFFileInputStream inStream(mcBackup.GetFullPath()); wxZipInputStream zipIn(inStream); std::auto_ptr<wxZipEntry> entry; while (entry.reset(zipIn.GetNextEntry()), entry.get() != NULL) if (!entry->GetName().Matches(_("META-INF*"))) if (!zipOut.CopyEntry(entry.release(), zipIn)) break; } // Modify the jar TaskStep(); // STEP 2 SetStatus(_("Installing mods - Adding mod files...")); for (ConstModIterator iter = modList->begin(); iter != modList->end(); iter++) { wxFileName modFileName = iter->GetFileName(); SetStatus(_("Installing mods - Adding ") + modFileName.GetFullName()); if (iter->IsZipMod()) { wxFFileInputStream modStream(modFileName.GetFullPath()); TransferZipArchive(modStream, zipOut); } else { wxFileName destFileName = modFileName; destFileName.MakeRelativeTo(m_inst->GetInstModsDir().GetFullPath()); wxFFileInputStream input(modFileName.GetFullPath()); zipOut.PutNextEntry(destFileName.GetFullPath()); zipOut.Write(input); } } // Recompress the jar TaskStep(); // STEP 3 SetStatus(_("Installing mods - Recompressing jar...")); m_inst->SetNeedsRebuild(false); }