Skip to content

Commit

Permalink
v1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Redstonneur1256 committed May 19, 2024
1 parent c06de58 commit 698762e
Show file tree
Hide file tree
Showing 47 changed files with 238 additions and 671 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/validate-gradle-wrapper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "Validate Gradle Wrapper"
on: [push, pull_request]

jobs:
validation:
name: "Validation"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: gradle/actions/wrapper-validation@v3
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import arc.input.KeyCode;
import fr.redstonneur1256.modlib.key.KeyBindManager;

// This class is defined as an enum similarly to the base game, it doesn't need to be
public enum ExampleKeyBinds implements KeyBinds.KeyBind {

demo(KeyCode.j, "example-mod");
Expand Down
8 changes: 4 additions & 4 deletions Example/src/fr/redstonneur1256/examplemod/MainMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import arc.util.CommandHandler;
import arc.util.Log;
import fr.redstonneur1256.examplemod.net.CustomCallExample;
import fr.redstonneur1256.examplemod.net.packet.direct.CustomPacketExample;
import fr.redstonneur1256.examplemod.net.packet.reply.CustomReplyPacketExample;
import fr.redstonneur1256.modlib.net.NetworkDebuggable;
import mindustry.gen.Groups;
import mindustry.gen.Player;
Expand All @@ -15,13 +17,11 @@ public void init() {
// Register custom keybindings (needed by all the examples, do not comment)
ExampleKeyBinds.register();

// Uncomment the demo you want to test

// Send custom packets to the server/client
// CustomPacketExample.init();
CustomPacketExample.init();

// Send custom packet and wait for reply, client/server example
// CustomReplyPacketExample.init();
CustomReplyPacketExample.init();

// Test if a String is foo using a custom Call implementation
CustomCallExample.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ public interface CustomCall {

}

public static void init() {
MVars.net.registerCall(CustomCall.class, new CustomCall() {
@Override
public CallResult<Boolean> isFoo(Player player, String text) {
System.out.println("isFoo() got called from player " + player.name + " with text " + text);
if ("foo".equals(text)) {
return CallResult.of(true);
}
// Exceptions can either be thrown directly or by returning CallResult#failed(Throwable)
throw new RuntimeException("it's not foo");
public static class CustomCallImplementation implements CustomCall {

@Override
public CallResult<Boolean> isFoo(Player player, String text) {
System.out.println("isFoo() got called from player " + player.name + " with text " + text);
if ("foo".equals(text)) {
return CallResult.of(true);
}
});
// Exceptions can either be thrown directly or by returning CallResult#failed(Throwable)
throw new RuntimeException("it's not foo");
}

}

public static void init() {
MVars.net.registerCall(CustomCall.class, new CustomCallImplementation());

// For the client side, every game tick:
Events.run(EventType.Trigger.update, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ public static void init() {
// Register a custom packet, for this example a simple packet containing a String
PacketManager.registerPacket(MessagePacket.class, MessagePacket::new);

/*
* You can either unregister packet handlers by doing
* 1:
* - MVars.net.unregisterClient(Class, Cons)
* - MVars.net.unregisterServer(Class, Cons2)
* 2:
* By registering your listener with
* - MVars.net.registerClient(Class, Cons)
* - MVars.net.registerServer(Class, Cons2)
* instead of Vars.net.handleClient/Server
* and then calling unregister()
*
* Note that you can also register multiple listeners per packet type
*/

// Handle that packet on the server side, here its just printing the packet content and the sender name
Vars.net.handleServer(MessagePacket.class,
(connection, packet) -> Log.info("Received custom message '@' from @", packet.message, connection.player.name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ private void openGame() throws Exception {
initMixin();
initAccessWidener();

System.setProperty("modlib.disableLogger", String.valueOf(!settings.get(Boolean.class, "modlib.logger", false)));

Mixins.addConfiguration("launcher.mixins.json");
loadModModifiers();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
package fr.redstonneur1256.modlib.launcher.mixins;

import arc.Events;
import arc.struct.ObjectSet;
import arc.struct.Seq;
import arc.struct.StringMap;
import arc.util.Log;
import arc.util.Strings;
import mindustry.Vars;
import mindustry.game.EventType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Mixin(Vars.class)
public class VarsMixin {

@Shadow
public static boolean loadedLogger;
public static @Shadow boolean loadedLogger;
public static @Shadow boolean headless;

@Shadow
public static boolean headless;
@Inject(method = "loadLogger", at = @At("HEAD"), cancellable = true)
private static void loadCustomLogger(CallbackInfo ci) {
if (loadedLogger || Boolean.getBoolean("modlib.disableLogger")) return;

/**
* @author Redstonneur1256
* @reason fully replace the built-in logger by our custom one
*/
@Overwrite
public static void loadLogger() {
if (loadedLogger) return;
ci.cancel();

StringMap simpleClassNames = new StringMap();
Seq<String> hiddenClasses = Seq.with("arc.util.Log");
ObjectSet<String> hiddenClasses = ObjectSet.with("arc.util.Log");

String[] levels = { "DEBUG", "INFO", "WARN", "ERROR", "NONE" };
String[] colors = { "royal", "green", "yellow", "scarlet", "gray" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class ArcSettings {
public static final byte TYPE_STRING = 4;
public static final byte TYPE_BINARY = 5;


private Map<String, Object> values;

public ArcSettings() {
Expand Down
2 changes: 1 addition & 1 deletion Mod/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dependencies {
// compileOnly because common and launcher classes are provided by the launcher
compileOnlyApi(project(":Launcher"))
compileOnly(libs.bundles.mindustry)
api(libs.bundles.mindustry)
}

tasks.register("packJar", Jar::class.java) {
Expand Down
6 changes: 6 additions & 0 deletions Mod/src/fr/redstonneur1256/modlib/ModLib.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.redstonneur1256.modlib;

import fr.redstonneur1256.modlib.launcher.LauncherInitializer;
import fr.redstonneur1256.modlib.launcher.ModLibLauncher;
import fr.redstonneur1256.modlib.net.MNet;
import fr.redstonneur1256.modlib.ui.MUI;
import mindustry.Vars;
Expand Down Expand Up @@ -28,4 +29,9 @@ public static String getVersion() {
return Vars.mods.getMod(ModLib.class).meta.version;
}

public static void restartOnExit(boolean reloadLauncher) {
ModLibLauncher.launcher.restartGame = true;
ModLibLauncher.launcher.fastRestart = !reloadLauncher;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Arrays;

import static mindustry.Vars.charset;
import static mindustry.Vars.*;

/**
* Event called when a user ping the Mindustry server, all fields lengths limit have been removed however
* it will break if the combined length of the fields versionType, name, description, map and custom gamemode
* exceeds 478 bytes or if a single string is more than 256 bytes
* Event called when a user ping the Mindustry server, vanilla field length limits does not apply. it is however recommended
* to respect them as going over might cause string values to be trimmed.
*/
public class ServerListPingEvent {

Expand Down Expand Up @@ -100,30 +100,35 @@ public void setDefaults() {
* @return the encoded server data
*/
public ByteBuffer writeServerData() {
ByteBuffer buffer = ByteBuffer.allocate(500);
ByteBuffer buffer = ByteBuffer.allocate(512);

writeString(buffer, name);
writeString(buffer, map);
int reservedSpace = 1 + 1 + 4 + 4 + 4 + 1 + 1 + 4 + 1 + (customGamemode != null ? 1 : 0);

writeString(buffer, name, buffer.remaining() - reservedSpace);
writeString(buffer, map, buffer.remaining() - reservedSpace);

buffer.putInt(playerCount);
buffer.putInt(wave);

buffer.putInt(versionBuild);
writeString(buffer, versionType);
writeString(buffer, versionType, buffer.remaining() - reservedSpace);

buffer.put((byte) gamemode.ordinal());
buffer.putInt(playerLimit);

writeString(buffer, description);
writeString(buffer, description, buffer.remaining() - reservedSpace);
if (customGamemode != null) {
writeString(buffer, customGamemode);
writeString(buffer, customGamemode, buffer.remaining() - reservedSpace);
}

return buffer;
}

private static void writeString(ByteBuffer buffer, String string) {
private static void writeString(ByteBuffer buffer, String string, int maxLength) {
byte[] bytes = string.getBytes(charset);
if (bytes.length > maxLength) {
bytes = Arrays.copyOf(bytes, Math.max(maxLength, 0));
}
buffer.put((byte) bytes.length);
buffer.put(bytes);
}
Expand Down
5 changes: 4 additions & 1 deletion Mod/src/fr/redstonneur1256/modlib/key/KeyBindManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
public class KeyBindManager {

public static void registerKeyBinds(KeyBinds.KeyBind... binds) {
((KeyBindAccessor) Core.keybinds).registerKeyBinds(binds);
// silently fail instead of crashing if a mod calls the method before the game is restarted
if (Core.keybinds instanceof KeyBindAccessor) {
((KeyBindAccessor) Core.keybinds).registerKeyBinds(binds);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class LauncherInitializer {

public static boolean isInitialized() {
return doesClassExist("fr.redstonneur1256.modlib.launcher.ModLibLauncher");
return doesClassExist("fr.redstonneur1256.modlib.launcher.ModLibLauncher") || Boolean.getBoolean("modlib.android-launcher-loaded");
}

public static boolean isBundledJVM() {
Expand Down Expand Up @@ -119,10 +119,8 @@ public static void initialize() {
Core.settings.load();
}

if (Vars.headless) {
System.exit(0);
}
Core.app.exit();
System.exit(process.exitValue());
} catch (Throwable throwable) {
// Startup failed, revert changes that could cause problems
Core.settings.setAutosave(true);
Expand Down
31 changes: 0 additions & 31 deletions Mod/src/fr/redstonneur1256/modlib/mixins/arc/EventsMixin.java

This file was deleted.

24 changes: 0 additions & 24 deletions Mod/src/fr/redstonneur1256/modlib/mixins/arc/TimerMixin.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
@Mixin(Player.class)
public class PlayerMixin implements NetworkDebuggable {

@Shadow
public transient NetConnection con;
public transient @Shadow NetConnection con;

@Override
public long getPing() {
Expand Down
6 changes: 2 additions & 4 deletions Mod/src/fr/redstonneur1256/modlib/mixins/gen/SoundsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
@Mixin(Sounds.class)
public class SoundsMixin {

@Shadow
private static IntMap<Sound> idToSound;
@Shadow
private static ObjectIntMap<Sound> soundToId;
private static @Shadow IntMap<Sound> idToSound;
private static @Shadow ObjectIntMap<Sound> soundToId;

@Inject(method = "<clinit>", at = @At("TAIL"))
private static void init(CallbackInfo ci) {
Expand Down
Loading

0 comments on commit 698762e

Please sign in to comment.