psiberx / cp2077-codeware

Cyberpunk 2077 library and framework for creating script mods.
MIT License
71 stars 12 forks source link

Crash on startup when using Reflection with LogChannel #16

Closed Roms1383 closed 5 months ago

Roms1383 commented 5 months ago

Hello!

Since I constantly keep forgetting to comment logs out in my releases, I tried to leverage Codeware's Reflection as follow:

module MyMod

public static func E(str: String) -> Void {
  let fun = Reflection.GetGlobalFunction(n"LogChannel");
  if IsDefined(fun) {
    fun.Call([ToVariant(n"DEBUG"), ToVariant(s"[MyMod] \(str)")]);
  }
}

Which lead to a crash on game startup, right after the "Cyberpunk 2077" animated logo.

What is more disturbing is that there's no Cyberpunk stacktrace Report / no log found in Codeware / successful compilation in Redscript with no apparent error / no error in RED4ext.

As I've stumbled upon weird bugs with e.g. booleans / temporary array in Redscript in the past, I also tried cleaning up my script as follow, which didn't change the outcome:

module MyMod

public static func E(str: String) -> Void {
  let fun = Reflection.GetGlobalFunction(n"LogChannel");
  let exists = IsDefined(fun);
  if exists {
    let args: array<Variant> = [ToVariant(n"DEBUG"), ToVariant(s"[MyMod] \(str)")];
    let _ = fun.Call(args);
  }
}

Thanks for your time, as always.

psiberx commented 5 months ago

The call should be fun.Call([n"DEBUG", AsRef(s"[MyMod] \(str)")]);. Argument compatibility check has been improved in 1.8.3. It should prevent crashes.