Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when run in development environment #1

Open
camilla-ett opened this issue Feb 7, 2017 · 3 comments
Open

Crash when run in development environment #1

camilla-ett opened this issue Feb 7, 2017 · 3 comments

Comments

@camilla-ett
Copy link
Owner

There are problems!!

	protected void load() {
		// ロード

		// 開発用
		if(DevMode.DEVMODE != DevMode.NOT_IN_DEV){
			startSearch(FileList.dirDevClasses, true);
			if(DevMode.DEVMODE == DevMode.DEVMODE_ECLIPSE){
				for(File f:FileList.dirDevIncludeClasses){
					startSearch(f, true);
				}
			}
		}

		startSearch(FileList.dirMods, false);
	}

	private void startSearch(File root, boolean dev){
		if(dev){
			if (root.isDirectory()) {
				// ディレクトリの解析
				decodeDirectory(root, root);
			} else {
				// Zipの解析
				decodeZip(root);
			}
			return;
		}

		// mods
		String mcv = (String) FMLInjectionData.data()[4];
		LittleMaidReengaged.Debug("MC %s", mcv);
		LittleMaidReengaged.Debug("START SEARCH MODS FOLDER");
		decodeDirectory(root, root);
		for (File lf : root.listFiles()) {
			if (lf.isFile() && (lf.getName().endsWith(".zip") || lf.getName().endsWith(".jar"))) {
				// パッケージ
				decodeZip(lf);
			} else if (lf.isDirectory()) {
				// ディレクトリの解析
				String md = FileClassUtil.getLinuxAntiDotName(lf.getAbsolutePath());
				if (md.endsWith("/")) {
					md = md.substring(0, md.length()-1);
				}

				LittleMaidReengaged.Debug("DIR SEARCH %s", md);
				String mf = FileClassUtil.getFileName(md);
				LittleMaidReengaged.Debug(" SPLICE %s", mf);
				if (mf.equals(mcv)) {
					LittleMaidReengaged.Debug("DEBUG START SEARCH DIVIDED FOLDER");
					startSearch(lf, false);
				}
			}
		}
	}

	private void decodeDirectory(File pfile, File pRoot) {
		try {
			FileList.COMMON_CLASS_LOADER.addURL(pRoot.toURI().toURL());
		} catch (MalformedURLException e) {
			return;
		}
		// ディレクトリ内のクラスを検索
		for (File lf : pfile.listFiles()) {
			if (lf.isFile()) {
				String lname = lf.getName();
				if (lname.indexOf(getPreFix()) >= 0 && lname.endsWith(".class")) {
					// 対象クラスファイルなのでロード
					//ディレクトリはパスを自動で治してくれないので、手動で。
					loadClass(FileClassUtil.getClassName(
							FileClassUtil.getLinuxAntiDotName(lf.getAbsolutePath()),
							FileClassUtil.getLinuxAntiDotName(pRoot.getAbsolutePath())));
				}
			}else{
				//ディレクトリの場合は中身も捜索
				decodeDirectory(lf, pRoot);
			}
		}
	}

	private void decodeZip(File pfile) {
		// zipファイルを解析
		try {
			// 多分いらんと思う…
			FileList.COMMON_CLASS_LOADER.addURL(pfile.toURI().toURL());
		} catch (MalformedURLException e) {
			return;
		}
		try {
			FileInputStream fileinputstream = new FileInputStream(pfile);
			ZipInputStream zipinputstream = new ZipInputStream(fileinputstream);
			ZipEntry zipentry;

			do {
				zipentry = zipinputstream.getNextEntry();
				if(zipentry == null) {
					break;
				}
				if (!zipentry.isDirectory()) {
					String lname = zipentry.getName();
					if (lname.indexOf(getPreFix()) >= 0 && lname.endsWith(".class")) {
						loadClass(zipentry.getName());
					}
				}
			} while(true);

			zipinputstream.close();
			fileinputstream.close();
		}
		catch (Exception exception) {
			LittleMaidReengaged.Debug("add%sZip-Exception.", getPreFix());
		}

	}

	private void loadClass(String pname) {
		String lclassname = "";
		// 対象ファイルをクラスとしてロード
		try {
			Package lpackage = LittleMaidReengaged.class.getPackage();
			lclassname = pname.endsWith(".class") ? pname.substring(0, pname.lastIndexOf(".class")) : pname;
			Class lclass;
			if(lpackage != null) {
	// TODO ★	lclassname = (new StringBuilder(String.valueOf(lpackage.getName()))).append(".").append(lclassname).toString();
				lclassname = lclassname.replace("/", ".");
// LMM_EntityModeManager でしか使ってないので暫定
				lclass = FileList.COMMON_CLASS_LOADER.loadClass(lclassname);
			} else {
				lclass = Class.forName(lclassname);
			}
			if (Modifier.isAbstract(lclass.getModifiers())) {
				return;
			}
			if (append(lclass)) {
				LittleMaidReengaged.Debug("get%sClass-done: %s", getPreFix(), lclassname);
			} else {
				LittleMaidReengaged.Debug("get%sClass-fail: %s", getPreFix(), lclassname);
			}
			/*
            if (!(MMM_ModelStabilizerBase.class).isAssignableFrom(lclass) || Modifier.isAbstract(lclass.getModifiers())) {
            	LittleMaidReengaged.Debug(String.format(String.format("get%sClass-fail: %s", pprefix, lclassname)));
                return;
            }

            MMM_ModelStabilizerBase lms = (MMM_ModelStabilizerBase)lclass.newInstance();
            pmap.put(lms.getName(), lms);
            LittleMaidReengaged.Debug(String.format("get%sClass-done: %s[%s]", pprefix, lclassname, lms.getName()));
            */
		}
		catch (Exception exception) {
			LittleMaidReengaged.Debug("get%sClass-Exception.(%s)", getPreFix(), lclassname);
			if(DevMode.DEVELOPMENT_DEBUG_MODE && LittleMaidReengaged.cfg_PrintDebugMessage) exception.printStackTrace();
		}
		catch (Error error) {
			LittleMaidReengaged.Debug("get%sClass-Error: %s", getPreFix(), lclassname);
			if(DevMode.DEVELOPMENT_DEBUG_MODE && LittleMaidReengaged.cfg_PrintDebugMessage) error.printStackTrace();
		}

	}
@camilla-ett
Copy link
Owner Author

indicate DEVMODE in net.blacklab.lmr.util.DevMode.java if development environment.

メイドモードの外部拡張を意識した設計なんだと思う。
内部的に持っているメイドモードの読み込みはもっときれいにしたい

@camilla-ett
Copy link
Owner Author

camilla-ett commented Feb 7, 2017

Default Model is also loaded in this way.
Refactor loading part refer to AM2.

メイドモードだけでなく、デフォルトモデルとかもこの方法で読み込んでるみたい。
AM2とかを参考に読み込み処理を書き直す

@camilla-ett
Copy link
Owner Author

Use ClassLoader.getResource() to fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant