RigsOfRods / rigs-of-rods

Main development repository for Rigs of Rods soft-body physics simulator
https://www.rigsofrods.org
GNU General Public License v3.0
1.01k stars 175 forks source link

AngelScript: fixed ref counting, added ProceduralRoad API #2930

Closed ohlidalp closed 1 year ago

ohlidalp commented 2 years ago

Status (last update 2022-10-14)

Many feats originally developed here got merged in https://github.com/RigsOfRods/rigs-of-rods/pull/2931. The object ref-counting system, originally created here, got perfected in it's own repo: https://github.com/only-a-ptr/RefCountingObject-AngelScript

New script features:

Reference counting fix

Previously, if a script kept a pointer (a 'handle' in AngelScript terminology) to BeamClass or VehicleAIClass after the vehicle was removed, it would crash. I studied how the reference counting system works and added a RefCountingObject base class which lets us keep track of the object both inside and outside of the script. It will help registering more objects to the script engine.

ohlidalp commented 2 years ago

Sneak preview of what's coming. This script tests the new procedural road APIs and will become an useful tool later. The window only appears in terrain editing mode (hotkey Ctrl+Y). terrain_editor-alpha1.as.zip I'm not committing the script until final, you can load it manually via RoR.cfg app_custom_scripts=terrain_editor.as obrazek

ohlidalp commented 2 years ago

It's all drawn by the script! terrain_editor-alpha2.as.zip

obrazek

tritonas00 commented 2 years ago

getting some gcc warnings:

/home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/RefCountingObject.h:100:5: warning: ‘void operator delete(void*)’ called on pointer ‘*(RoR::Actor**)((char*)__first)’ with nonzero offset 8 [-Wfree-nonheap-object]
  100 |     }

Hm, can't load any script:

14:39:24: ScriptEngine initializing ...
14:39:24: Type registrations done. If you see no error above everything should be working
14:39:24:  (0, 0): Info = void ProceduralObjectClass::addPoint(procedural_point)
14:39:24:  (0, 0): Error = Don't support passing type 'procedural_point' by value to application in native calling convention on this platform
14:39:24:  (0, 0): Info = void ProceduralObjectClass::insertPoint(int, procedural_point)
14:39:24:  (0, 0): Error = Don't support passing type 'procedural_point' by value to application in native calling convention on this platform
14:39:28:  (0, 0): Error = Invalid configuration. Verify the registered application interface.
14:39:28:  (0, 0): Error = Invalid configuration. Verify the registered application interface.
14:39:53:  (0, 0): Error = Invalid configuration. Verify the registered application interface.
ohlidalp commented 2 years ago

free-nonheap-object? I didn't intend that, must double check. Might be a false alarm, tho: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54202

I didn't expect a problem with calling conventions... But apparently linux x64 is more complicated: https://www.gamedev.net/forums/topic/712220-native-calling-on-linux-x86-64-not-working-for-a-type/

ohlidalp commented 1 year ago

The work is almost done.

The RefCountingObject system works, except there's a breaking nuissance AngelScript side - you can't do game.getPlayerTruck().getTruckName() because you'll get error 'BeamClassPtr has no functon getTruckName()'. You need to do this instead:

BeamClass@ truck = game.getPlayerTruck();
truck.getTruckName(); // now it works!

I updated the terrain editor script. To test, just put it to "Documents/My Games/Rigs of Rods/scripts" and in the game console, say 'loadscript terrain_editor.as'.

terrain_editor-alpha4.as.zip

tritonas00 commented 1 year ago

Crashed as soon as i spawned a desperado in f1 testtrack:

Thread 1 "RoR" received signal SIGSEGV, Segmentation fault.
0x000055555b132320 in ?? ()
(gdb) bt
#0  0x000055555b132320 in ?? ()
#1  0x0000555555676d9a in RefCountingObject<RoR::Terrain>::Release (this=0x555557ee2b38)
    at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:33
#2  RefCountingObject<RoR::Terrain>::Release (this=0x555557ee2b38)
    at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:28
#3  RefCountingObjectPtr<RoR::Terrain>::ReleaseHandle (this=<optimized out>)
    at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:198
#4  RefCountingObjectPtr<RoR::Terrain>::~RefCountingObjectPtr (this=<optimized out>, __in_chrg=<optimized out>)
    at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:190
#5  RoR::GfxScene::UpdateScene (this=0x555555c497e0 <RoR::App::g_gfx_scene>, dt_sec=dt_sec@entry=0.378546089)
    at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/gfx/GfxScene.cpp:142
#6  0x00005555555f5cfb in main (argc=<optimized out>, argv=<optimized out>)
    at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/main.cpp:101

Also still got massive spam about:

In destructor ‘RefCountingObject<T>::~RefCountingObject() [with T = RoR::Actor]’,
    inlined from ‘void RefCountingObject<T>::Release() [with T = RoR::Actor]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:33:13,
    inlined from ‘void RefCountingObject<T>::Release() [with T = RoR::Actor]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:28:10,
    inlined from ‘void RefCountingObjectPtr<T>::ReleaseHandle() [with T = RoR::Actor]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:198:23,
    inlined from ‘RefCountingObjectPtr<T>::~RefCountingObjectPtr() [with T = RoR::Actor]’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObjectPtr.h:190:18,
    inlined from ‘void RoR::GfxActor::UpdateParticles(float)’ at /home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/gfx/GfxActor.cpp:533:25:
/home/babis/Downloads/ror-dependencies/rigs-of-rods/source/main/utils/memory/RefCountingObject.h:21:5: warning: ‘void operator delete(void*)’ called on pointer ‘<anonymous>.RefCountingObjectPtr<RoR::Actor>::m_ref’ with nonzero offset 8 [-Wfree-nonheap-object]
   21 |     }
ohlidalp commented 1 year ago

Purpose: To do AngelScript right - currently the object refcounting is dummy and prone to crash.

Estimate: 8 man days. This is hard work but it's an once-and-for-all fix which needs to be done at some point.

Work to be done:

  1. backport changes made here to https://github.com/only-a-ptr/RefCountingObject-AngelScript
  2. replicate the problem there
  3. get help from AngelScript author
ohlidalp commented 1 year ago

I'm closing this because it got stale and it was too invasive to begin with. I'll start a new PR with less code and more added value.