eldenring 0.14.0

Structures, bindings, and utilities for From Software's title Elden Ring
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::ptr::NonNull;
use std::sync::LazyLock;
use std::thread;
use std::time::{Duration, Instant};

use pelite::pe64::Pe;
use vtable_rs::VPtr;
use windows::core::PCWSTR;

use crate::dlkr::DLPlainConditionSignal;
use crate::fd4::{FD4TaskBase, FD4TaskBaseVmt, FD4TaskData};
use crate::{
    DLVector,
    dlkr::DLPlainLightMutex,
    fd4::{FD4BasicHashString, FD4Time},
    rva,
    util::system::{SystemInitError, wait_for_system_init},
};
use shared::{
    FromStatic, InstanceError, OwnedPtr, RecurringTask, SharedTaskImp, Subclass, Superclass,
    program::Program,
};

#[vtable_rs::vtable]
pub trait CSEzTaskVmt: FD4TaskBaseVmt {
    /// Called by execute() in the case of CSEzTask.
    fn eztask_execute(&mut self, data: &FD4TaskData);
    /// Called to register the task to the appropriate runtime.
    fn register_task(&mut self);
    /// Called to clean up the task.
    fn free_task(&mut self);
}

#[vtable_rs::vtable]
pub trait CSEzTaskProxyVmt: FD4TaskBaseVmt {
    fn get_task_group(&self) -> CSTaskGroupIndex;
}

#[repr(C)]
#[derive(Subclass, Superclass)]
#[subclass(base = FD4TaskBase)]
pub struct CSEzTask {
    pub vftable: VPtr<dyn CSEzTaskVmt, Self>,
    unk8: u32,
    _padc: u32,
    pub task_proxy: NonNull<CSEzTaskProxy>,
}

#[repr(C)]
#[derive(Subclass, Superclass)]
#[subclass(base = CSEzTask, base = FD4TaskBase)]
pub struct CSEzRabbitTaskBase {
    pub ez_task: CSEzTask,
    unk18: u32,
    _pad1c: u32,
}

#[repr(C)]
#[derive(Subclass)]
#[subclass(base = CSEzRabbitTaskBase, base = CSEzTask, base = FD4TaskBase)]
pub struct CSEzRabbitNoUpdateTask {
    pub ez_rabbit_task_base: CSEzRabbitTaskBase,
}

/// Often used by the game to periodically run some update on a structure.
#[repr(C)]
pub struct CSEzUpdateTask<TEzTask, TSubject> {
    pub base_task: TEzTask,

    /// Whatever this update task is operating on
    pub subject: NonNull<TSubject>,

    /// Takes in the subject and the delta time
    pub executor: fn(&TSubject, &FD4Time),
}

#[repr(C)]
#[derive(Subclass)]
#[subclass(base = CSEzRabbitTaskBase, base = CSEzTask, base = FD4TaskBase)]
pub struct CSEzRabbitTask {
    pub base: CSEzRabbitTaskBase,
}

// This can't implement Subclass because there's no reliable way to find the
// correct vtable for any given set of generics.
#[repr(C)]
pub struct CSEzVoidTask<TEzTask, TSubject>
where
    TEzTask: Subclass<CSEzTask>,
{
    pub base_task: TEzTask,

    /// Whatever this update task is operating on
    pub subject: NonNull<TSubject>,

    /// Takes in the subject and the delta time
    pub executor: fn(&TSubject, f32),
}

#[repr(C)]
#[derive(Subclass)]
#[subclass(base = FD4TaskBase)]
pub struct CSEzTaskProxy {
    vftable: VPtr<dyn CSEzTaskProxyVmt, Self>,
    unk8: u32,
    _padc: u32,
    pub task: Option<NonNull<CSEzTask>>,
}

#[repr(C)]
#[shared::singleton("CSTaskGroup")]
pub struct CSTaskGroup {
    vftable: usize,
    pub task_groups: [OwnedPtr<CSTimeLineTaskGroupIns>; 168],
}

#[repr(C)]
#[derive(Superclass)]
pub struct CSTaskGroupIns {
    vftable: usize,
    pub name: FD4BasicHashString,
    unk40: [u8; 0x10],
}

#[repr(C)]
#[derive(Subclass)]
pub struct CSTimeLineTaskGroupIns {
    pub base: CSTaskGroupIns,
    pub step_impl: usize,
    unk60: [u8; 0x20],
}

/// The task manager that schedules tasks to run at various points during the
/// rendering of the frame.
///
/// This is thread-safe, so it's safe to call [FromStatic::instance] outside the
/// main thread.
#[repr(C)]
#[shared::singleton("CSTask")]
pub struct CSTaskImp {
    vftable: usize,
    pub inner: OwnedPtr<CSTask>,
}

impl CSTaskImp {
    /// Blocks this thread until the singleton instance is available.
    ///
    /// **Note:** This should never be called on the main thread, since the main
    /// thread is responsible for initializing this singleton in the first
    /// place.
    ///
    /// This returns [`SystemInitError::InvalidRva`] if either the global
    /// HINSTANCE RVA or the `SprjTaskImp` RVA aren't within the executable.
    ///
    /// [`SystemInitError::InvalidRva`]: SystemInitError::InvalidRva
    pub fn wait_for_instance(timeout: Duration) -> Result<&'static Self, SystemInitError> {
        let start = Instant::now();
        wait_for_system_init(&Program::current(), timeout)?;

        loop {
            match unsafe { Self::instance() } {
                Err(InstanceError::NotFound(_)) => return Err(SystemInitError::InvalidRva),
                Err(InstanceError::Null(_)) => {
                    if start.elapsed() > timeout {
                        return Err(SystemInitError::Timeout);
                    }
                    thread::yield_now();
                }
                Ok(instance) => return Ok(instance),
            }
        }
    }
}

// TODO: Implement this by directly manipulating the CSTaskImp's vectors rather
// than relying on hooking through an AOB.
static REGISTER_TASK_VA: LazyLock<u64> = LazyLock::new(|| {
    Program::current()
        .rva_to_va(rva::get().register_task)
        .expect("Call target for REGISTER_TASK was not in exe")
});

impl SharedTaskImp<CSTaskGroupIndex, FD4TaskData> for CSTaskImp {
    fn register_task_internal(&self, index: CSTaskGroupIndex, task: &RecurringTask<FD4TaskData>) {
        let register_task: extern "C" fn(
            &CSTaskImp,
            CSTaskGroupIndex,
            &RecurringTask<FD4TaskData>,
        ) = unsafe { std::mem::transmute(*REGISTER_TASK_VA) };
        register_task(self, index, task);
    }
}

#[repr(C)]
#[derive(Superclass)]
pub struct CSTaskBase {
    vftable: usize,
    allocator: usize,
    pub task_groups: DLVector<TaskGroupEntry>,
    pub task_group_index_max: u32,
    _pad34: u32,
}

#[repr(C)]
pub struct TaskGroupEntry {
    pub index: u32,
    pub name: [u16; 64],
    pub active: bool,
}

#[repr(C)]
#[derive(Subclass)]
pub struct CSTask {
    pub task_base: CSTaskBase,
    allocator: usize,
    unk40: usize,
    unk48: [usize; 3],
    unk60: [usize; 3],
    pub task_runner_manager: OwnedPtr<CSTaskRunnerManager>,
    pub task_runners: [OwnedPtr<CSTaskRunner>; 6],
    pub task_runners_ex: [OwnedPtr<CSTaskRunnerEx>; 6],
    unke0: usize,
}

#[repr(C)]
pub struct CSTaskRunner {
    vftable: usize,
    task_queue: usize,
    pub task_runner_manager: OwnedPtr<CSTaskRunnerManager>,
    unk18: u32,
    _pad1c: u32,
    unk_string: PCWSTR,
}

#[repr(C)]
pub struct CSTaskRunnerEx {
    // TODO
}

#[repr(C)]
pub struct CSTaskRunnerManager {
    allocator: usize,
    pub concurrent_task_group_count: usize,
    pub concurrent_task_group_policy: OwnedPtr<TaskGroupConcurrency>,
    pub current_concurrent_task_group: u32,
    unk1c: u32,
    unk20: u32,
    _pad24: u32,
    pub mutex: DLPlainLightMutex,
    pub signals: [DLPlainConditionSignal; 6],
    unkb8: u32,
    unkbc: u32,
    unkc0: u32,
    unkc4: u32,
    unkc8: u32,
    unkcc: u32,
    unkd0: u32,
    unkd4: u32,
}

#[repr(C)]
pub struct TaskGroupConcurrency {
    pub slots: [TaskGroupConcurrencySlot; 6],
}

#[repr(C)]
pub struct TaskGroupConcurrencySlot {
    pub task_group_index: u32,
    pub task_group_concurrency_type: u32,
}

#[repr(u32)]
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy)]
pub enum CSTaskGroupIndex {
    FrameBegin,
    SteamThread0,
    SteamThread1,
    SteamThread2,
    SteamThread3,
    SteamThread4,
    SteamThread5,
    SystemStep,
    ResStep,
    PadStep,
    GameFlowStep,
    EndShiftWorldPosition,
    GameMan,
    TaskLineIdx_Sys,
    TaskLineIdx_Test,
    TaskLineIdx_NetworkFlowStep,
    TaskLineIdx_InGame_InGameStep,
    TaskLineIdx_InGame_InGameStayStep,
    MovieStep,
    RemoStep,
    TaskLineIdx_InGame_MoveMapStep,
    FieldArea_EndWorldAiManager,
    EmkSystem_Pre,
    EmkSystem_ConditionStatus,
    EmkSystem_Post,
    EventMan,
    FlverResDelayDelectiionBegin,
    TaskLineIdx_InGame_FieldAreaStep,
    TaskLineIdx_InGame_TestNetStep,
    TaskLineIdx_InGame_InGameMenuStep,
    TaskLineIdx_InGame_TitleMenuStep,
    TaskLineIdx_InGame_CommonMenuStep,
    TaskLineIdx_FrpgNet_Sys,
    TaskLineIdx_FrpgNet_Lobby,
    TaskLineIdx_FrpgNet_ConnectMan,
    TaskLineIdx_FrpgNet_Connect,
    TaskLineIdx_FrpgNet_Other,
    SfxMan,
    FaceGenMan,
    FrpgNetMan,
    NetworkUserManager,
    SessionManager,
    BlockList,
    LuaConsoleServer,
    RmiMan,
    ResMan,
    SfxDebugger,
    REMOTEMAN,
    Geom_WaitActivateFade,
    Geom_UpdateDraw,
    Grass_BatchUpdate,
    Grass_ResourceLoadKick,
    Grass_ResourceLoad,
    Grass_ResourceCleanup,
    WorldChrMan_Respawn,
    WorldChrMan_Prepare,
    ChrIns_CalcUpdateInfo_PerfBegin,
    ChrIns_CalcUpdateInfo,
    ChrIns_CalcUpdateInfo_PerfEnd,
    WorldChrMan_PrePhysics,
    WorldChrMan_CalcOmissionLevel_Begin,
    WorldChrMan_CalcOmissionLevel,
    WorldChrMan_CalcOmissionLevel_End,
    WorldChrMan_ConstructUpdateList,
    WorldChrMan_ChrNetwork,
    ChrIns_Prepare,
    ChrIns_NaviCache,
    ChrIns_AILogic_PerfBegin,
    ChrIns_AILogic,
    ChrIns_AILogic_PerfEnd,
    AI_SimulationStep,
    ChrIns_PreBehavior,
    ChrIns_PreBehaviorSafe,
    GeomModelInsCreatePartway_Begin,
    HavokBehavior,
    GeomModelInsCreatePartway_End,
    ChrIns_BehaviorSafe,
    ChrIns_PrePhysics_Begin,
    ChrIns_PrePhysics,
    ChrIns_PrePhysics_End,
    NetFlushSendData,
    ChrIns_PrePhysicsSafe,
    ChrIns_RagdollSafe,
    ChrIns_GarbageCollection,
    GeomModelInsCreate,
    AiBeginCollectGabage,
    WorldChrMan_Update_RideCheck,
    InGameDebugViewer,
    LocationStep,
    LocationUpdate_PrePhysics,
    LocationUpdate_PrePhysics_Parallel,
    LocationUpdate_PrePhysics_Post,
    LocationUpdate_PostCloth,
    LocationUpdate_PostCloth_Parallel,
    LocationUpdate_PostCloth_Post,
    LocationUpdate_DebugDraw,
    EventCondition_BonfireNearEnemyCheck,
    HavokWorldUpdate_Pre,
    RenderingSystemUpdate,
    HavokWorldUpdate_Post,
    ChrIns_PreCloth,
    ChrIns_PreClothSafe,
    HavokClothUpdate_Pre_AddRemoveRigidBody,
    HavokClothUpdate_Pre_ClothModelInsSafe,
    HavokClothUpdate_Pre_ClothModelIns,
    HavokClothUpdate_Pre_ClothManager,
    CameraStep,
    DrawParamUpdate,
    GetNPAuthCode,
    SoundStep,
    HavokClothUpdate_Post_ClothManager,
    HavokClothUpdate_Post_ClothModelIns,
    HavokClothVertexUpdateFinishWait,
    ChrIns_PostPhysics,
    ChrIns_PostPhysicsSafe,
    CSDistViewManager_Update,
    HavokAi_SilhouetteGeneratorHelper_Begin,
    WorldChrMan_PostPhysics,
    GameFlowInGame_MoveMap_PostPhysics_0,
    HavokAi_SilhouetteGeneratorHelper_End,
    DmgMan_Pre,
    DmgMan_ShapeCast,
    DmgMan_Post,
    GameFlowInGame_MoveMap_PostPhysics_1_Core0,
    GameFlowInGame_MoveMap_PostPhysics_1_Core1,
    GameFlowInGame_MoveMap_PostPhysics_1_Core2,
    MenuMan,
    WorldChrMan_Update_BackreadRequestPre,
    ChrIns_Update_BackreadRequest,
    WorldChrMan_Update_BackreadRequestPost,
    HavokAi_World,
    WorldAiManager_BeginUpdateFormation,
    WorldAiManager_EndUpdateFormation,
    GameFlowInGame_TestNet,
    GameFlowInGame_InGameMenu,
    GameFlowInGame_TitleMenu,
    GameFlowInGame_CommonMenu,
    GameFlowFrpgNet_Sys,
    GameFlowFrpgNet_Lobby,
    GameFlowFrpgNet_ConnectMan,
    GameFlowFrpgNet_Connect,
    GameFlowStep_Post,
    ScaleformStep,
    FlverResDelayDelectiionEnd,
    Draw_Pre,
    GraphicsStep,
    DebugDrawMemoryBar,
    DbgMenuStep,
    DbgRemoteStep,
    PlaylogSystemStep,
    ReviewMan,
    ReportSystemStep,
    DbgDispStep,
    DrawStep,
    DrawBegin,
    GameSceneDraw,
    AdhocDraw,
    DrawEnd,
    Draw_Post,
    SoundPlayLimitterUpdate,
    BeginShiftWorldPosition,
    FileStep,
    FileStepUpdate_Begin,
    FileStepUpdate_End,
    Flip,
    DelayDeleteStep,
    AiEndCollectGabage,
    RecordHeapStats,
    FrameEnd,
}