Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,20 @@ private <T> void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.
}

private void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, int level, int datapointer, DataType value) {
String modificationId = modification.getVal();
MetaFieldId field = MetaFieldId.valueOf(modification.getVal());
// Replace every existing value of this field. Fields read from the object file of a type without levels
// (units, items, destructables, buffs) are plain mods and occupy the only slot there is.
// (units, items, destructables, buffs) are plain mods and occupy the only slot there is. The object keeps
// its mods by field, so only this field's are looked at; an ability with many levels has thousands.
List<ObjMod.Obj.Mod> replaced = new ArrayList<>();
for (ObjMod.Obj.Mod m : od.getMods()) {
if (!m.getId().getVal().equals(modificationId)) {
continue;
}
for (ObjMod.Obj.Mod m : od.getModsOfField(field)) {
if (m instanceof ObjMod.Obj.ExtendedMod extMod
&& (extMod.getLevel() != level || extMod.getDataPt() != datapointer)) {
continue;
}
replaced.add(m);
}
replaced.forEach(od::remove);
od.addMod(new ObjMod.Obj.ExtendedMod(MetaFieldId.valueOf(modificationId), variableType, value, level, datapointer));
od.addMod(new ObjMod.Obj.ExtendedMod(field, variableType, value, level, datapointer));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,19 +330,19 @@ boolean registerCreatedObjectDefinition(String fileExtension, String objId) {
}

/**
* Calculate hash of an object file's contents
* The object file as it is written into the map. It is serialised once and both hashed and written from
* these bytes: a big map's object files take a noticeable time to serialise.
*/
private String calculateObjectFileHash(ObjMod<? extends ObjMod.Obj> dataStore) {
private byte[] serializeObjectFile(ObjMod<? extends ObjMod.Obj> dataStore) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Wc3BinOutputStream out = new Wc3BinOutputStream(baos);
dataStore.write(out, ObjMod.EncodingFormat.AS_DEFINED);
out.close();
byte[] data = baos.toByteArray();
return ImportFile.calculateHash(data);
return baos.toByteArray();
} catch (Exception e) {
WLogger.warning("Could not calculate object file hash: " + e.getMessage());
return String.valueOf(System.currentTimeMillis());
WLogger.severe(e);
throw new Error(e);
}
}

Expand Down Expand Up @@ -418,15 +418,15 @@ public void writeBack(boolean inject) {
}

// Compute hash of what we intend to write
String currentHash = calculateObjectFileHash(dataStore);
byte[] currentContent = serializeObjectFile(dataStore);
String currentHash = ImportFile.calculateHash(currentContent);
dataStoreHashes.put(fileType, currentHash);

boolean mpqHasSame = false;
if (mpqEditor != null && mpqEditor.hasFile("war3map." + fileType.getExt())) {
try {
byte[] existing = mpqEditor.extractFile("war3map." + fileType.getExt());
String existingHash = ImportFile.calculateHash(existing);
mpqHasSame = existingHash.equals(currentHash);
mpqHasSame = Arrays.equals(existing, currentContent);
} catch (Exception e) {
WLogger.info("Could not validate MPQ content for " + fileType.getExt() + ": " + e.getMessage());
}
Expand All @@ -447,7 +447,7 @@ public void writeBack(boolean inject) {

WLogger.info("Object file " + fileType.getExt() + " changed or MPQ out of sync, writing back");
filesUpdated++;
writebackObjectFile(dataStore, fileType, inject);
writebackObjectFile(currentContent, dataStore, fileType, inject);
anyFileWritten = anyFileWritten || inject;
if (inject) {
newManifest.putEntry(fileType.getExt(), currentHash, dataStore.getObjsList().size());
Expand Down Expand Up @@ -489,17 +489,11 @@ private void writeW3oFile() {
}
}

private void writebackObjectFile(ObjMod<? extends ObjMod.Obj> dataStore, ObjectFileType fileType, boolean inject) throws Error {
private void writebackObjectFile(byte[] w3_, ObjMod<? extends ObjMod.Obj> dataStore, ObjectFileType fileType,
boolean inject) throws Error {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Wc3BinOutputStream out = new Wc3BinOutputStream(baos);
Optional<File> folder = getObjectEditingOutputFolder();

dataStore.write(out, ObjMod.EncodingFormat.AS_DEFINED);

out.close();
byte[] w3_ = baos.toByteArray();

exportToWurst(dataStore, fileType, new File(folder.get(), "WurstExportedObjects_" + fileType.getExt() + ".wurst.txt").toPath());

if (inject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@ public class NativeJassFunction implements ExecutableJassFunction {
private final String name;
private final Method method;
private final Object provider;
/** {@link Method#getParameterTypes} copies the array on every call, and this is asked on every native call. */
private final Class<?>[] parameterTypes;
private final int nameParameters;

public NativeJassFunction(String name, Object provider, Method method) {
this.name = name;
this.provider = provider;
this.method = method;
this.parameterTypes = method.getParameterTypes();
this.nameParameters = parameterTypes.length > 0 && parameterTypes[0] == String.class ? 1 : 0;
}

Class<?>[] parameterTypes() {
return parameterTypes;
}

/**
* 1 when the method's first parameter is a String: it implements several natives, and is passed the name of the
* one called before the native's own arguments.
*/
int nameParameters() {
Class<?>[] declared = method.getParameterTypes();
return declared.length > 0 && declared[0] == String.class ? 1 : 0;
return nameParameters;
}

/** The arguments the method is invoked with: the native's, after the native's name when the method takes it. */
Expand All @@ -44,7 +52,7 @@ public ILconst execute(JassInterpreter jassInterpreter, ILconst[] arguments) {
int offset = nameParameters();
for (int i = 0; i < arguments.length; i++) {
if (arguments[i] instanceof ILconstNull
&& !method.getParameterTypes()[i + offset].isAssignableFrom(arguments[i].getClass())) {
&& !parameterTypes[i + offset].isAssignableFrom(arguments[i].getClass())) {
arguments[i] = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.peeeq.wurstscript.intermediatelang.ILconst;
import de.peeeq.wurstscript.intermediatelang.interpreter.NativesProvider;
import de.peeeq.wurstscript.intermediatelang.interpreter.NoSuchNativeException;
import de.peeeq.wurstscript.utils.Pair;

import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -17,21 +16,32 @@ public abstract class ReflectionBasedNativeProvider implements NativesProvider {

protected PrintStream outStream = System.err;

private final HashMap<Pair<String, Integer>, Method> methodMap = new HashMap<>();
/** The methods by name, then by their number of parameters, so that a call allocates no key. */
private final HashMap<String, Method[]> methodMap = new HashMap<>();

public ReflectionBasedNativeProvider() {
for (Method method : this.getClass().getMethods()) {
Pair<String, Integer> keyPair = Pair.create(method.getName(), method.getParameterTypes().length);
if (methodMap.containsKey(keyPair)) {
int arity = method.getParameterCount();
Method[] byArity = methodMap.get(method.getName());
if (byArity == null || byArity.length <= arity) {
Method[] grown = new Method[arity + 1];
if (byArity != null) {
System.arraycopy(byArity, 0, grown, 0, byArity.length);
}
byArity = grown;
methodMap.put(method.getName(), byArity);
}
if (byArity[arity] != null) {
throw new RuntimeException("native entry already exists");
}
methodMap.put(keyPair, method);
byArity[arity] = method;
}
}

@Override
public ILconst invoke(String funcname, ILconst[] args) throws NoSuchNativeException {
Method method = methodMap.get(Pair.create(funcname, args.length));
Method[] byArity = methodMap.get(funcname);
Method method = byArity != null && args.length < byArity.length ? byArity[args.length] : null;
if (method == null) {
String msg = "Calling method " + funcname + "(" +
Arrays.stream(args).map(Object::toString).collect(Collectors.joining(", ")) + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,18 @@ public ILconst invoke(String funcname, ILconst[] args) throws NoSuchNativeExcept
}

// A method whose first parameter is a String implements several natives and is told which one was called.
Class<?>[] declared = candidate.getMethod().getParameterTypes();
Class<?>[] declared = candidate.parameterTypes();
int offset = candidate.nameParameters();
if (declared.length == args.length + offset) {
String[] parameterTypes = new String[args.length];
for (int i = 0; i < args.length; i++) {
parameterTypes[i] = "" + args[i];
if (!declared[i + offset].isAssignableFrom(args[i].getClass())) {
if (args[i] instanceof ILconstNull) {
// handle null as a special case and pass it to the native as a Java null
args[i] = null;
continue;
}
throw new Error("The native <" + funcname + "> expects different parameter " + i + "!" +
"\n\tExpected: " + declared[i + offset].getSimpleName() + " Actual: " + parameterTypes[i]);
"\n\tExpected: " + declared[i + offset].getSimpleName() + " Actual: " + args[i]);
}
}
}
Expand Down
Loading