Jump to content

Recommended Posts

mdhClassReplace.jar is a simple javaprogram to replace classes in all ext, sqs, sqf, sqm files in the folder and subfolders of the toollocation. Set classnames in the mdhClassReplace.cfg file.

i wrote it to replace all the A2 classnames of my arma 2 missions with the CUP ones. So the tool scans all ext, sqs, sqm, sqf files and replace the classnames with the ones set in the mdhClassReplace.cfg.

mdhClassReplace.cfg.png

mdhClassReplaceLog.txt.png

mdhClassReplace.jar Source:

package mdhClassReplace;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class mdhClassReplace {

	public static void dirTree(File dir, List classOld, List classNew) {
		File[] subdirs = dir.listFiles();
		for (File subdir : subdirs) {
			if (subdir.isDirectory()) {
				dirTree(subdir, classOld, classNew);
			} else {
				doFile(subdir, classOld, classNew);
			}
		}
	}

	public static void doFile(File file, List classOld, List classNew) {
		String extension = "";
		int i = file.getName().lastIndexOf('.');
		if (i > 0) {
			extension = file.getName().substring(i + 1).toLowerCase();
		}

		if (extension.contentEquals("sqm")
				|| extension.contentEquals("sqf")
				|| extension.contentEquals("sqs")
				|| extension.contentEquals("ext")) {
			writeLog("checkFile: " + file.getAbsolutePath());
			rplTxt(file.getAbsolutePath(), classOld, classNew);
		}
	}

	public static void rplTxt(String fileName, List classOld, List classNew) {

		Path path = Paths.get(fileName);
		Charset charset = StandardCharsets.UTF_8;

		String content = null;

		try {
			content = new String(Files.readAllBytes(path), charset);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String contentLower = content.toLowerCase();

		boolean foundClass = false;
		for (int i = 0; i < classOld.size(); i++) {
			String classOldS = "\"" + classOld.get(i).toString() + "\"";
			String classNewS = "\"" + classNew.get(i).toString() + "\"";
			if (contentLower.contains(classOldS.toLowerCase())) {
				foundClass = true;
				content = content.replaceAll("(?i)"+classOldS, classNewS);
				writeLog("changedClass: " + classOldS + " to " + classNewS);								
			}
			
			classOldS = "'" + classOld.get(i).toString() + "'";
			classNewS = "'" + classNew.get(i).toString() + "'";
			if (contentLower.contains(classOldS.toLowerCase())) {
				foundClass = true;
				content = content.replaceAll("(?i)"+classOldS, classNewS);
				writeLog("changedClass: " + classOldS + " to " + classNewS);
			}
		}

		if (foundClass) {
			try {
				Files.write(path, content.getBytes(charset));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void writeLog(String logTxt) {
		PrintWriter out;
		logTxt = new java.util.Date() + " --> " + logTxt;
		try {
			out = new PrintWriter(new BufferedWriter(new FileWriter(
					"mdhClassReplaceLog.txt", true)));
			out.println(logTxt);
			System.out.println(logTxt);
			out.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	public static void main(String[] args) {

		writeLog("-------------------------------");
		writeLog("pgmStart");
		String currentDir = System.getProperty("user.dir");
		writeLog("currentDir: " + currentDir);

		File f = new File(currentDir);
		File c = new File(currentDir.concat("\\mdhClassReplace.cfg"));

		if (c.exists() && !c.isDirectory()) {

			String classOldS = "";
			String classNewS = "";
			List classOld = new ArrayList();
			List classNew = new ArrayList();

			try {
				BufferedReader in = new BufferedReader(new FileReader(c));
				String actLine = null;
				while ((actLine = in.readLine()) != null) {

					int seperatorPos = actLine.indexOf(";");
					classOldS = actLine.substring(0, seperatorPos);
					classNewS = actLine.substring(seperatorPos + 1);

					if (!classOldS.isEmpty() && !classNewS.isEmpty()) {
						classOld.add(classOldS);
						classNew.add(classNewS);
					}

				}
			} catch (IOException e) {
				e.printStackTrace();
				writeLog("pgmError: error while reading mdhClassReplace.cfg");
				System.exit(-1);
			}

			dirTree(f, classOld, classNew);
		} else {
			writeLog("mdhClassReplace.cfg not found");
		};
		
		writeLog("pgmEnd");
	}
}

how does it work:

1. create and write log messages into the mdhClassReplaceLog.txt

2. read the mdhClassReplace.cfg to get the old and new classnames for the replacement

3. check every ext, sqs, sqm, sqf file in the folder and subfolders of the toollocation for the old classnames and replace them with the new one

how to use it, example to port A2 mission to A3 with CUP:

1. download the mdhClassReplace.7z

2. extract it to an empty folder

3. put your arma 2 missions into the same folder

4. doubleklick the mdhClassReplace.jar file

5. check the mdhClassReplaceLog.txt file to see what the tool has done

6. copy your arma 2 mission into your arma 3 missions folder and open it in the 3D editor (hopefully it works :D)

attention, use this tool/mdhClassReplace.jar on your own risk

Download: http://moerderhoschi.bplaced.net/public/tools/arma3/mdhClassReplace.7z

my A2 -> CUP ClassnameMSExcelWar  :D  -> http://moerderhoschi.bplaced.net/public/tools/arma3/ArmAClassnames.png

updates:

v1.1

- fixed issue with case sensitivity at classname replace -> now the class replacement is case insensitiv

- added more classes to replace to the mdhClassReplace.cfg

v1.2

- added more replacements to the mdhClassReplace.cfg

- update of Javaprogram .sqs and .ext files now also checked along with .sqm and .sqf

kind regards

moerderhoschi

  • Like 2

Share this post


Link to post
Share on other sites

Nice tool :). I don't suppose you have a setup to port an OFP mission to CUP?

Share this post


Link to post
Share on other sites

@R0adki11 sry no i haven't. It was more work to gather the right CUP classnames to replace the A2 originals as to write the replacement java tool :D

  • Like 1

Share this post


Link to post
Share on other sites

nice one!

what regex do you use so far?

if useful, i could dig up my regex and my mappings i've used for OFP->A1->A2

Share this post


Link to post
Share on other sites

what regex do you use so far?

i search for the classname with doublequotes and replace it in the same way

"\"" + classOld.get(i).toString() + "\""; -> "M1A1" become "CUP_B_M1A1_Woodland_USMC"
"'" + classOld.get(i).toString() + "'"; -> 'M1A1' become 'CUP_B_M1A1_Woodland_USMC'
 

if useful, i could dig up my regex and my mappings if used for OFP->A1->A2

nice idea, the big work would be to feed the mdhClassReplace.cfg with all the classnames of OFP,A1,A2 and the matching replacements of A3CUP,A3,...

Share this post


Link to post
Share on other sites

alright. will see if i can find it again :)

i was using more context. with scripts and scripting missions i was getting too much false matches

(mainly as BI used names like m21, m1a1, etc and people didn't tag variables back then; markers and file names were a problem too)

Share this post


Link to post
Share on other sites

update to v1.1:

- fixed issue with case sensitivity at classname replace -> now the class replacement is case insensitiv

- added more classes to replace to the mdhClassReplace.cfg

mdhClassReplace.jar Source also updated in the first post

  • Like 1

Share this post


Link to post
Share on other sites

https://cdn.discordapp.com/attachments/181805221054251008/227101570322530305/arma_project_script_conversion.7z- contains OFP to WGL and OFP to A1 mapping

https://cdn.discordapp.com/attachments/181805221054251008/227101637150244864/arma_project_mission_conversion.7z- regex for sqs/OFP scripts to sqf/A1 standards

https://cdn.discordapp.com/attachments/181805221054251008/227110291438174208/oac_core.7z- config contains somewhat a mapping of A1 to A2

Open the pga files with powerGREP. It has an extended free trial with full feature-set.

The best regex tool with nice interface and preview. Also fast.

Otherwise drag them into a text/code editor, and extract the regex. If you have questions, best send me a PM or contact me in discord.

  • Like 2

Share this post


Link to post
Share on other sites

@foxhound thank you for hosting on armaholic

@.kju thank you for sharing your work but i think here is a little mistake. i wrote the tool/javaPgm to port some of my A2 missions to A3 with CUP and released the tool for sharing it with the community. but i can not spend more time to adding all classes and the replacements^^

maybe now and then i will add some classes to the mdhClassReplace.cfg in the process porting some of my A2 missions, but i dont have the time to start now a big project with gathering all classes and the replacements. this is a work which can be done by someone with more freeTime/lifeTime :D

sry for raising wrong hopes

  • Like 1

Share this post


Link to post
Share on other sites

you misunderstood me sharing those - after all anyone can make the making from OFP/A1/A2 to A3 now fairly easily

the mappings are already done

in regards to the regex in there - they are to help you make your tool more reliable and catch "all" instances of classes

  • Like 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×