Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
更新LOG机制
Browse files Browse the repository at this point in the history
  • Loading branch information
wohaopa committed May 8, 2023
1 parent 05038ca commit 371b72e
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Main {
private static String zpLaunch = "http://127.0.0.1/ZeroPointLaunch/Library/";
private static File rootDir;
private static String zpLaunch;
private static final File libDir;

public static List<String> lib = new ArrayList<>();
private static final List<String> lib = new ArrayList<>();

private static final Properties config = new Properties();

static {
// 懒得找了,写死得了
Expand All @@ -29,23 +32,56 @@ public class Main {
lib.add("hamcrest-core-1.3.jar");
lib.add("ZeroPointLaunch-Core.jar");

String userDir = System.getProperty("user.dit");
rootDir = new File(userDir, "lib");
String userDir = System.getProperty("user.dir");
libDir = new File(userDir, "lib");
File configFile = new File(userDir + "/config.properties");
if (!configFile.exists()) {
config.setProperty("download-url", "http://127.0.0.1/ZeroPointLaunch/Library/");
config.setProperty("check-libraries", "true");
config.setProperty("check-update", "true");
try {
config.store(new FileOutputStream(configFile), null);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
config.load(new FileInputStream(configFile));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

public static void main(String[] args) {

lib.forEach(s -> {
File libFile = new File(rootDir, s);
if (!libFile.exists()) {
System.out.println("missing...: " + s);
try {
downloadFile(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
zpLaunch=config.getProperty("download-url","http://127.0.0.1/ZeroPointLaunch/Library/");

if (config.getProperty("check-update", "false").equals("true")) {
File newCore = new File(libDir, "ZeroPointLaunch-Core-new.jar");
if (newCore.exists()) {
File curCore = new File(libDir, "ZeroPointLaunch-Core.jar");
File oldCore = new File(libDir, "ZeroPointLaunch-Core-old.jar");

if (oldCore.exists()) oldCore.delete();
if (curCore.exists()) curCore.renameTo(oldCore);
newCore.renameTo(curCore);
}
});
}

if (config.getProperty("check-libraries", "false").equals("true")) {
lib.forEach(s -> {
File libFile = new File(libDir, s);
if (!libFile.exists()) {
System.out.println("missing...: " + s);
try {
downloadFile(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}


try {
Expand Down Expand Up @@ -82,9 +118,9 @@ private static void downloadFile(String s) throws IOException {
byte[] getData = readInputStream(inputStream);

//文件保存位置
rootDir.mkdirs();
libDir.mkdirs();

File file = new File(rootDir + File.separator + s);
File file = new File(libDir + File.separator + s);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);

Expand Down
59 changes: 42 additions & 17 deletions src/main/java/com/github/wohaopa/zeropointlanuch/api/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.github.wohaopa.zeropointlanuch.core.*;
import com.github.wohaopa.zeropointlanuch.core.utils.DownloadUtil;
import com.github.wohaopa.zeropointlanuch.core.utils.FileUtil;

public class Core {

Expand All @@ -53,32 +54,32 @@ public static void installStandard(File zipFile, File dir, String name, String v

/** 检查实例文件夹 */
public static void lookup() {
Log.LOGGER.debug("[文件初始化]实例搜寻:开始");
Log.start("文件初始化");
Instance.clear();
for (File file : Objects.requireNonNull(DirTools.instancesDir.listFiles())) {
if (file.isDirectory()) {
File version = new File(file, "version.json");
if (version.exists()) {
Log.LOGGER.debug("[文件初始化]实例搜寻:发现实例" + file.getName());
Log.debug("发现实例" + file.getName());
InstanceInstaller.addInst(version);
}
}
}
Log.LOGGER.debug("[文件初始化]实例搜寻:结束");
Log.LOGGER.debug("[文件初始化]安装包搜寻:开始");
Log.end();
Log.start("文件初始化");
for (File file : Objects.requireNonNull(DirTools.zipDir.listFiles())) {
if (file.isFile() && file.getName()
.endsWith(".zip")) {
String name = file.getName();
name = name.replace("GT_New_Horizons_", "");
name = name.replace("_Client.zip", "");
if (!Instance.containsKey(name)) {
Log.LOGGER.debug("[文件初始化]实例搜寻:发现安装包 {},名为 {}", file.getName(), name);
Log.debug("发现安装包 {},名为 {}", file.getName(), name);
InstanceInstaller.installStandard(file, new File(DirTools.instancesDir, name), name, name);
}
}
}
Log.LOGGER.debug("[文件初始化]安装包搜寻:结束");
Log.end();
}

/**
Expand Down Expand Up @@ -108,22 +109,46 @@ public static void downloadFile(String url) {
try {
DownloadUtil.takeDownloadResult();
} catch (ExecutionException | InterruptedException e) {
Log.LOGGER.error("文件下载失败:{}", url);
Log.error("文件下载失败:{}", url);
}
}

public static String genRuntimeDir(Instance inst, Sharer sharer) {
inst.genRuntimeDir(sharer);
return inst.information.runDir;
}
/*
* public static String genHMCLDir(String name) {
* Instance inst = Instance.get(name);
* inst.genRuntimeDir();
* FileUtil.genLink(new File(inst.runDir, "assets"), DirTools.assetsDir);
* FileUtil.genLink(new File(inst.runDir, "libraries"), DirTools.librariesDir);
* FileUtil.genLink(new File(inst.runDir, "versions"), DirTools.versionsDir);
* return inst.information.runDir;
* }
*/

public static void genTransferOld(Instance instance, File file) {

List<String> exclude = new ArrayList<>();
for (File file1 : Objects.requireNonNull(file.listFiles())) {
if (file1.getName()
.startsWith(".")) exclude.add(file1.getPath());
}
exclude.add(file.getPath() + "\\assets");
exclude.add(file.getPath() + "\\libraries");
exclude.add(file.getPath() + "\\logs");
exclude.add(file.getPath() + "\\versions");

long a = System.currentTimeMillis();
Identification identification = new Identification(instance.information.checksum);
Differ differ = Differ.diff(file, identification, exclude);
long b = System.currentTimeMillis();
Log.debug("对比用时:{}s", (b - a) / 1000.0);

File outDir = new File(instance.insDir, "private-outDir");
if (outDir.exists()) {
File oldOutDir = new File(instance.insDir, "private-outDir-old");
FileUtil.delete(oldOutDir);
if (!outDir.renameTo(oldOutDir)) FileUtil.delete(outDir);
} else {
outDir.mkdir();
}
differ.addition.forEach(s -> {
File f1 = new File(file, s);
File f2 = new File(outDir, s).getParentFile();
if (!f2.exists()) f2.mkdirs();
FileUtil.copyDir(f1, f2);
});
}
}
33 changes: 26 additions & 7 deletions src/main/java/com/github/wohaopa/zeropointlanuch/core/Differ.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,54 @@
package com.github.wohaopa.zeropointlanuch.core;

import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Checksum;

import com.github.wohaopa.zeropointlanuch.core.utils.FileUtil;

public class Differ {

public static Differ diff(File dir, Identification identification) {
public void setExclude(List<String> exclude) {
this.exclude = exclude;
}

public static Differ diff(File dir, Identification identification, List<String> exclude) {

Differ differ = new Differ(dir.getPath() + "\\");
Differ differ = new Differ(dir.getPath() + "\\", exclude);
differ.search(dir, identification.item);
return differ;
}

public final List<String> removed = new ArrayList<>();
public final List<String> addition = new ArrayList<>();
private String rootStr;
private final String rootStr;
private List<String> exclude;

public Differ(String rootStr) {
public Differ(String rootStr, List<String> exclude) {
this.rootStr = rootStr;
this.exclude = exclude;
}

public void search(File dir, Identification.IdentificationDirectoryItem directoryItem) {
private void search(File dir, Identification.IdentificationDirectoryItem directoryItem) {

File[] rootDir = dir.listFiles();
if (rootDir == null || rootDir.length < 1) return;
List<String> loaded = new ArrayList<>();
for (File file : rootDir) {
String fileName = file.getName();
if (fileName.equals("Quests")) continue; // 跳过任务书检测

if (exclude != null) {
boolean flag = false;
for (String s : exclude) if (file.getPath()
.endsWith(s)) {
flag = true;
exclude.remove(s);
break;
}
if (flag) continue;
}

if (directoryItem.contains(fileName)) { // 镜像内存在相应名字的文件或者目录
loaded.add(fileName);
Identification.IdentificationItem identificationItem = directoryItem.getFile(fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DirTools {
public static File versionsDir;

public static void init(File workDir) {
Log.LOGGER.info("目录管理初始化开始");
Log.start("目录管理");
DirTools.workDir = workDir;
DirTools.instancesDir = FileUtil.initAndMkDir(workDir, "instances");
DirTools.librariesDir = FileUtil.initAndMkDir(workDir, "libraries");
Expand All @@ -49,6 +49,6 @@ public static void init(File workDir) {
DirTools.shareDir = FileUtil.initAndMkDir(workDir, "share");
DirTools.tmpDir = FileUtil.initAndMkDir(workDir, "tmpDir");
DirTools.versionsDir = FileUtil.initAndMkDir(workDir, "versions");
Log.LOGGER.info("目录管理初始化结束");
Log.end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ private Instance() {}

/** 加载实例信息,仅在初始化阶段使用本方法来加载持久化的实例 */
public Information loadInformation() {
Log.LOGGER.debug("[实例加载]正在加载:{}", versionFile);
Log.debug("正在加载:{}", versionFile);
return JsonUtil.fromJson(FileUtil.fileRead(versionFile), Information.class);
}

/** 保存实例信息 */
public void savaInformation() {
Log.LOGGER.debug("[实例安装]正在保存:{}", versionFile);
Log.debug("正在保存:{}", versionFile);
FileUtil.fileWrite(versionFile, JsonUtil.toJson(information));
}

Expand All @@ -108,7 +108,7 @@ public void genRuntimeDir(Sharer sharer) {

depVersion = instance.information.depVersion;
} else {
Log.LOGGER.error("未知版本:" + depVersion);
Log.error("未知版本:" + depVersion);
throw new RuntimeException("未知版本:" + depVersion);
}
}
Expand All @@ -127,7 +127,7 @@ private void copyFileAsLink(Mapper mapper, List<String> excludeModsList) {
if (excludeModsList.contains(mods)) continue; // 排除
File modsFile = new File(modsDir, mods);
if (!modsFile.exists()) {
Log.LOGGER.fatal("缺失mod:{}", modsFile);
Log.error("缺失mod:{}", modsFile);
throw new RuntimeException("缺失mod:" + modsFile);
}
FileUtil.genLink(new File(modsRun, modsFile.getName()), modsFile);
Expand Down
Loading

0 comments on commit 371b72e

Please sign in to comment.