ATCS Beanshell scripts

--------------------------------------------------
---- LIST PROJECTS AND GET INDICES ---------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.model.Workspace;
import com.gpl.rpg.atcontentstudio.model.ProjectTreeNode;
for (ProjectTreeNode proj : Workspace.activeWorkspace.projects) {
	print(proj.getDesc()+" - "+Workspace.activeWorkspace.projects.indexOf(proj));
}
--------------------------------------------------
---- LIST PROJECTS AND SHOW SOURCE DIR -----------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.model.Workspace;
import com.gpl.rpg.atcontentstudio.model.ProjectTreeNode;
import com.gpl.rpg.atcontentstudio.model.Project;
for (ProjectTreeNode proj : Workspace.activeWorkspace.projects) {
	if (proj instanceof Project) {
		print(Workspace.activeWorkspace.projects.indexOf(proj)+" - "+proj.getDesc()+" - "+proj.baseContent.baseFolder.getAbsolutePath());
	}
	else {
		print(Workspace.activeWorkspace.projects.indexOf(proj)+" - "+proj.getDesc()+" - [Open project to look up source path]");
	}
}
--------------------------------------------------
---- CHANGE PROJECT SOURCE DIR -------------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.model.Workspace;
import com.gpl.rpg.atcontentstudio.model.ProjectTreeNode;
import com.gpl.rpg.atcontentstudio.model.Project;
///RISKY !!!///
proj = Workspace.activeWorkspace.projects.get(0);
proj.baseContent.baseFolder = new File("/path/to/new/folder");
proj.save();
--------------------------------------------------
---- QUEST COMPLETION TEST -----------------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.model.Workspace;
proj = Workspace.activeWorkspace.projects.get(0);
for(quest : proj.baseContent.gameData.quests) {
	print(quest.id+"\t--> "+quest.name);
	completing_stages_count = 0;
	for (stage : quest.stages) {
		if (stage.finishes_quest != null && stage.finishes_quest != 0) {
			completing_stages_count++;
		}
	}
	if (completing_stages_count == 0) {
		print("ALERT: Impossible to finish this quest");
	} else if (completing_stages_count == 1) {
		print("\tThere is 1 way to complete this quest");	
	} else {
		print("\tThere are "+completing_stages_count+" ways to complete this quest");	
	}
	print("");
}
--------------------------------------------------
---- SEARCH IN DIALOGUES AND REPLIES -------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.*;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.gamedata.*;
proj = Workspace.activeWorkspace.projects.get(1);
String searched = "my brother";
int repl_count = 0;
int dial_count = 0;
for (int i = 0; i < proj.getDialogueCount(); i++) {
	Dialogue dial = proj.getDialogue(i);
	if (dial == null  || dial.message == null) continue;
	if (dial.replies == null || dial.replies.isEmpty()) continue;
	Dialogue.Reply reply;
	for (reply : dial.replies) {
		if (reply.text == null) continue;
		if (reply.text.indexOf(searched) > 0) {
			repl_count++;
			print("-----Match in reply-------");
			print(dial.id);
			print("- "+dial.message);
			print("- "+reply.text);
			if (reply.next_phrase != null && reply.next_phrase.message != null) {
				print("- "+reply.next_phrase.message);
			}
		}	
	}
	if (dial.message.indexOf(searched) > 0) {
		dial_count++;
		print("-----Match in dialogue ----");
			print(dial.id);
			print("- "+dial.message);
	}
}
print ("-------------------------");
print (dial_count+" match(es) in dialogues");
print (repl_count+" match(es) in replies");
print ("-------------------------");
--------------------------------------------------
---- SEARCH IN MAP NAMES -------------------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.*;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.gamedata.*;
import com.gpl.rpg.atcontentstudio.model.maps.*;
proj = Workspace.activeWorkspace.projects.get(1);
String searched = "church";
int repl_count = 0;
for (int i = 0; i < proj.getMapCount(); i++) {
	TMXMap map = proj.getMap(i);
	if (map == null  || map.id == null) continue;
	if (map.id.indexOf(searched) > 0) {
			repl_count++;
			print(map.id);
	}
	
}
print ("-------------------------");
print (repl_count+" match(es)");
print ("-------------------------");
--------------------------------------------------
---- SPAWN AREAS WITH DUPLICATE IDS --------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.*;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.gamedata.*;
import com.gpl.rpg.atcontentstudio.model.maps.*;
import java.util.List;
import java.util.LinkedList;
proj = Workspace.activeWorkspace.projects.get(1);
int repl_count = 0;
for (int i = 0; i < proj.getMapCount(); i++) {
	TMXMap map = proj.getMap(i);
	if (map == null  || map.groups == null) continue;
	List spawnAreaNames = new LinkedList();
	for(MapObjectGroup group : map.groups) {
		if (group.mapObjects == null || group.mapObjects.isEmpty()) continue;
		for (MapObject obj : group.mapObjects) {
			if (!(obj instanceof SpawnArea)) continue;
			if (spawnAreaNames.contains(obj.name)) {
				print(map.id+" -> "+obj.name);
			} else {
				spawnAreaNames.add(obj.name);
			}
		}
	}
	
}
--------------------------------------------------
---- REMOVE DUPLICATE IDS IN SPAWN AREAS ---------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.*;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.gamedata.*;
import com.gpl.rpg.atcontentstudio.model.maps.*;
import java.util.Map;
import java.util.LinkedHashMap;
import java.io.File;
import java.io.FileWriter;
proj = Workspace.activeWorkspace.projects.get(1);
int repl_count = 0;
for (int i = 0; i < proj.getMapCount(); i++) {
	TMXMap map = proj.getMap(i);
	if (map == null  || map.groups == null) continue;
	Map spawnAreaNames = new LinkedHashMap();
	boolean dupl = false;
	for(MapObjectGroup group : map.groups) {
		if (group.mapObjects == null || group.mapObjects.isEmpty()) continue;
		for (MapObject obj : group.mapObjects) {
			if (!(obj instanceof SpawnArea)) continue;
			if (spawnAreaNames.containsKey(obj.name)) {
				int index = ((Integer)spawnAreaNames.get(obj.name)) + 1;
				spawnAreaNames.put(obj.name,index);
				obj.name = obj.name+"_"+index;
				print(map.id+" -> "+obj.name);
				dupl = true;
			} else {
				spawnAreaNames.put(obj.name,0);
			}
		}
	}
	if (dupl) {
		File tmxFile = new File("/tmp/maps/"+map.id+".tmx");
		FileWriter w = new FileWriter(tmxFile);
		w.write(map.toXml());
		w.close();
	}
}
--------------------------------------------------
---- RESAVE EVERYTHING ---------------------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.*;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.gamedata.*;
foo() {
	run() {
		
Project proj = Workspace.activeWorkspace.projects.get(4);
print("Project - "+proj.getDesc());
for (ProjectTreeNode category : proj.baseContent.gameData.children()) {
	print("\tCategory - "+category.getDesc());
	for (ProjectTreeNode element : category.children()) {
		//System.out.println("\t\t"+element.getDesc());
		print("\t\t"+element.id);
		if (proj.getGameDataElement(element.getClass(), element.id) == element) {
			proj.makeWritable(element);
			element = proj.getGameDataElement(element.getClass(), element.id);
			element.link();
			element.save();
		}
	}
	//System.out.println();
}
	}
	return this;
}
foo = foo();
new Thread(foo).start();
--------------------------------------------------
---- SCAN DEPENDENCIES ---------------------------
--------------------------------------------------
import com.gpl.rpg.atcontentstudio.*;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.ui.tools.*;
import com.gpl.rpg.atcontentstudio.model.gamedata.*;
proj = Workspace.activeWorkspace.projects.get(1);
print("Project: "+proj.getDesc());
obj = proj.getQuest("roots_love");
print("Obj: "+obj.getDesc());
dependencies = GDEVisitor.findDependencies(obj, false);
print("Scan complete");
print("");
for (dependency : dependencies) {
	print(dependency.getDataType().toString()+"/"+dependency.getClass().getSimpleName()+"/"+dependency.id);
}