; WWMI BETA-2 INI

; Mod State -------------------------

; Global variables used by entire mod

[Constants]
; Allows WWMI to safely disable incompatible mod and notify user about it
global $required_wwmi_version = 0.96
; Number of indices in original model
global $object_guid = 241500
; Number of vertices in custom model
global $mesh_vertex_count = 6604
; Number of shapekeyed vertices in custom model
global $shapekey_vertex_count = 6604
; ID assigned to our mod by WWMI
global $mod_id = -1000
; Controls whether our mod is enabled, prevents any overrides from happening if $mod_enabled == 0
; Prevents user from being crash-locked in case of incompatible WWMI version
global $mod_enabled = 0
; Indicates if our object was detected in previous frame
global $object_detected = 0
global persist $ActiveCharacter
global persist $key11 = 1
global persist $houdang = 1

[key1]
condition= $object_detected
key=4
type = cycle
$houdang= 0,1
global persist $ActiveCharacter

[key11]
condition= $object_detected
key=,
type = cycle
$key11= 0,1



[Present]
if $object_detected
    if $mod_enabled
        post $object_detected = 0
    else
        ; Check if our mod is compatible with installed WWMI version (runs only once)
        if $mod_id == -1000
            ; Pass required WWMI version along with mod metadata to WWMI
            run = CommandListRegisterMod
        endif
    endif
endif
post $ActiveCharacter = 0


; Contacts WWMI to check whether installed version is compatible with our mod
[CommandListRegisterMod]
; Pass mod info variables to WWMI
$\WWMIv1\required_wwmi_version = $required_wwmi_version
$\WWMIv1\object_guid = $object_guid
; Pass mod info resources to WWMI
Resource\WWMIv1\ModName = ref ResourceModName
Resource\WWMIv1\ModAuthor = ref ResourceModAuthor
Resource\WWMIv1\ModDesc = ref ResourceModDesc
Resource\WWMIv1\ModLink = ref ResourceModLink
Resource\WWMIv1\ModLogo = ref ResourceModLogo
; Register mod in WWMI
run = CommandList\WWMIv1\RegisterMod
; Read mod_id assigned to our mod by WWMI, incompatible mod will get `$mod_id == -1` assigned
$mod_id = $\WWMIv1\mod_id
; Enable our mod if WWMI assigned valid $mod_id to it
if $mod_id >= 0
    $mod_enabled = 1
endif

; Resources: Mod Info -------------------------

; Name of mod
[ResourceModName]
type = Buffer
data = "1"

; Name of mod author
[ResourceModAuthor]
type = Buffer
data = "1"

; Mod description
[ResourceModDesc]
type = Buffer
data = "1"

; Link to mod repository
[ResourceModLink]
type = Buffer
data = "1"

; Texture file with 512x512 .dds (BC7 SRGB) mod logo
[ResourceModLogo]
; filename = Textures/Logo.dds

; Shading: Draw Call Stacks Processing -------------------------

; Overrides textures via triggering [ResourceTextureX] sections by calling chechtextureoverride on ps-t slots
[CommandListTriggerResourceOverrides]
; Trigger texture sections to replace texture with matching hash
CheckTextureOverride = ps-t0
CheckTextureOverride = ps-t1
CheckTextureOverride = ps-t2
CheckTextureOverride = ps-t3
CheckTextureOverride = ps-t4
CheckTextureOverride = ps-t5
CheckTextureOverride = ps-t6
CheckTextureOverride = ps-t7

; Overrides resources that are shared between VS calls
[CommandListOverrideSharedResources]
; Store pointer to original buffer to restore original context on cleanup
ResourceBypassVB0 = ref vb0
; Override resources to make draw calls use custom meshes
ib = ResourceIndexBuffer
vb0 = ResourcePositionBuffer
vb1 = ResourceVectorBuffer
vb2 = ResourceTexcoordBuffer
vb3 = ResourceColorBuffer
vb4 = ResourceBlendBuffer

; Overrides resources that are shared between VS calls
[CommandListCleanupSharedResources]
; Restore original vb0 so we can use its hash to match next call
vb0 = ref ResourceBypassVB0

; Override draw calls for Component 0
[TextureOverrideComponent0]
hash = 8a382bf1
match_first_index = 0
match_index_count = 7518
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 1
[TextureOverrideComponent1]
hash = 8a382bf1
match_first_index = 7518
match_index_count = 53454
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 2
[TextureOverrideComponent2]
hash = 8a382bf1
match_first_index = 60972
match_index_count = 12090
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 3
[TextureOverrideComponent3]
hash = 8a382bf1
match_first_index = 73062
match_index_count = 55536
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 4
[TextureOverrideComponent4]
hash = 8a382bf1
match_first_index = 128598
match_index_count = 93894
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 5
[TextureOverrideComponent5]
hash = 8a382bf1
match_first_index = 222492
match_index_count = 1548
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 6
[TextureOverrideComponent6]
hash = 8a382bf1
match_first_index = 224040
match_index_count = 228
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    ; run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    ; run = CommandListOverrideSharedResources
    ; Draw skipped: No matching custom components found
    ; Restore required parts of original context
    ; run = CommandListCleanupSharedResources
endif

; Override draw calls for Component 7
[TextureOverrideComponent7]
hash = 8a382bf1
match_first_index = 224268
match_index_count = 17232
; Signal our mod that object is found on screen and we can start overriding it
$object_detected = 1
if $mod_enabled
    ; Skip original draw call
    handling = skip
    ; Trigger by-hash resource overrides
    run = CommandListTriggerResourceOverrides
    ; Do by-slot resource overrides
    run = CommandListOverrideSharedResources
	if $houdang == 1
       if $key11 == 1
       ; Draw Component 7.0_17232_0
	   drawindexed = 17232, 0, 0
       endif
    endif
    if $houdang == 0

	   if $key11 == 1
       ; Draw Component 7.1_17232_0
	   drawindexed = 17232, 17232, 0
       endif
    endif
    ; Restore required parts of original context
    run = CommandListCleanupSharedResources
endif

; Shading: Textures -------------------------

[ResourceTexture0]
filename = Textures/Components-0 t=420bc474.dds

[TextureOverrideTexture0]
hash = 420bc474
match_priority = 0
if $object_detected
    this = ResourceTexture0
endif

[ResourceTexture1]
filename = Textures/Components-0 t=83b787db.dds

[TextureOverrideTexture1]
hash = 83b787db
match_priority = 0
if $object_detected
    this = ResourceTexture1
endif

[ResourceTexture2]
filename = Textures/Components-0-1-2-3-4 t=44a33623.dds

[TextureOverrideTexture2]
hash = 44a33623
match_priority = 0
if $object_detected
    this = ResourceTexture2
endif

[ResourceTexture3]
filename = Textures/Components-1 t=2eb74e79.dds

[TextureOverrideTexture3]
hash = 2eb74e79
match_priority = 0
if $object_detected
    this = ResourceTexture3
endif

[ResourceTexture4]
filename = Textures/Components-2 t=301efc44.dds

[TextureOverrideTexture4]
hash = 301efc44
match_priority = 0
if $object_detected
    this = ResourceTexture4
endif

[ResourceTexture5]
filename = Textures/Components-2 t=697a05d5.dds

[TextureOverrideTexture5]
hash = 697a05d5
match_priority = 0
if $object_detected
    this = ResourceTexture5
endif

[ResourceTexture6]
filename = Textures/Components-3 t=3973960b.dds

[TextureOverrideTexture6]
hash = 3973960b
match_priority = 0
if $object_detected
    this = ResourceTexture6
endif

[ResourceTexture7]
filename = Textures/Components-3 t=67787504.dds

[TextureOverrideTexture7]
hash = 67787504
match_priority = 0
if $object_detected
    this = ResourceTexture7
endif

[ResourceTexture8]
filename = Textures/Components-3-4-7 t=f47bc395.dds

[TextureOverrideTexture8]
hash = f47bc395
match_priority = 0
if $object_detected
    this = ResourceTexture8
endif

[ResourceTexture9]
filename = Textures/Components-4 t=11c042bc.dds

[TextureOverrideTexture9]
hash = 11c042bc
match_priority = 0
if $object_detected
    this = ResourceTexture9
endif

[ResourceTexture10]
filename = Textures/Components-4 t=3db825ca.dds

[TextureOverrideTexture10]
hash = 3db825ca
match_priority = 0
if $object_detected
    this = ResourceTexture10
endif

[ResourceTexture11]
filename = Textures/Components-4 t=94e015b4.dds

[TextureOverrideTexture11]
hash = 94e015b4
match_priority = 0
if $object_detected
    this = ResourceTexture11
endif

[ResourceTexture12]
filename = Textures/Components-5 t=8383cbbf.dds

[TextureOverrideTexture12]
hash = 8383cbbf
match_priority = 0
if $object_detected
    this = ResourceTexture12
endif

[ResourceTexture13]
filename = Textures/Components-5 t=ec8277c1.dds

[TextureOverrideTexture13]
hash = ec8277c1
match_priority = 0
if $object_detected
    this = ResourceTexture13
endif

[ResourceTexture14]
filename = Textures/Components-6 t=913631e7.dds

[TextureOverrideTexture14]
hash = 913631e7
match_priority = 0
if $object_detected
    this = ResourceTexture14
endif

[ResourceTexture15]
filename = Textures/Components-7 t=0cf861f8.dds

[TextureOverrideTexture15]
hash = 0cf861f8
match_priority = 0
if $object_detected
    this = ResourceTexture15
endif

[ResourceTexture16]
filename = Textures/Components-7 t=35fc907b.dds

[TextureOverrideTexture16]
hash = 35fc907b
match_priority = 0
if $object_detected
    this = ResourceTexture16
endif

; Skinning: Shape Keys Override -------------------------

; Increases size of UAV that stores shapekeyed vertices xyz offsets to support more vertices than original
[TextureOverrideShapeKeyOffsets]
hash = 5c9d8190
match_priority = 0
override_byte_stride = 24
override_vertex_count = $mesh_vertex_count

; Increases size of UAV that stores shapekeyed vertices multipliers to support more vertices than original
[TextureOverrideShapeKeyScale]
hash = cbe0b3ad
match_priority = 0
override_byte_stride = 4
override_vertex_count = $mesh_vertex_count

; Updates ResourceShapeKeyCBRW that stores offsets of shapekeyed vertex lists, shape key values and multipliers
[CommandListSetupShapeKeys]
; Pass 4 byte checksum of shapekey offsets to ensure that we only modify expected values
$\WWMIv1\shapekey_checksum = 2010
; Pass buffer with offsets for vertex lists of every shape key of custom model
cs-t33 = ResourceShapeKeyOffsetBuffer
; Pass buffer with custom values for every shape key, allows to control both custom and stock
cs-u5 = ResourceCustomShapeKeyValuesRW
; Pass buffer that gonna store result of calculations, required for Shape Key Loader CS to run
cs-u6 = ResourceShapeKeyCBRW
; Run ShapeKeyOverrider CS
run = CustomShader\WWMIv1\ShapeKeyOverrider

; Runs custom Shape Key Loader CS to, well, load shapekeys data from buffers
[CommandListLoadShapeKeys]
; Pass number of shapekeyed vertices to adjust required threads count via dipatch_y
$\WWMIv1\shapekey_vertex_count = $shapekey_vertex_count
; Pass buffer with lists of per-vertex ids for every shape key
cs-t0 = ResourceShapeKeyVertexIdBuffer
; Pass buffer with lists of xyz per-vertex offsets for every shape key
cs-t1 = ResourceShapeKeyVertexOffsetBuffer
; Pass buffer with shape key vertex lists offsets, and shape key values & multipliers
cs-u6 = ResourceShapeKeyCBRW
; Run ShapeKeyLoader CS
run = CustomShader\WWMIv1\ShapeKeyLoader

; Handles WWMI callback fired on original Shape Key Loader CS call
[TextureOverrideShapeKeyLoaderCallback]
hash = 5c9d8190
match_priority = 0
if $mod_enabled
    ; Ensure that callback has WWMI filter_index of Shape Key Loader CS assigned
    if cs == 3381.3333
        ; It looks like we can skip checking for THREAD_GROUP_COUNT_Y as UAV hashes are unique for each object
        ; Ensures that dispatch_y of Shape Key Loader CS call matches one from dump
        ; if THREAD_GROUP_COUNT_Y == 1112
        ; Skip handling of original Shape Key Loader CS call to modify dispatch_y value
        handling = skip
        ; Run custom Shape Key Overrider CS to prepare shape key resources for loading
        run = CommandListSetupShapeKeys
        ; Run custom Shape Key Loader CS to load shape key resources
        run = CommandListLoadShapeKeys
        ; endif
    endif
endif

; Runs custom Shape Key Loader CS to, well, load shapekeys data from buffers
[CommandListMultiplyShapeKeys]
; Pass number of shapekeyed vertices to adjust required threads count via dipatch_y
$\WWMIv1\custom_vertex_count = $mesh_vertex_count
; Run custom Shape Key Multiplier CS to set deformation intensity
run = CustomShader\WWMIv1\ShapeKeyMultiplier

; Handles WWMI callback fired on original Shape Key Multiplier CS call
[TextureOverrideShapeKeyMultiplierCallback]
hash = 5c9d8190
match_priority = 0
if $mod_enabled
    ; Ensure that callback has WWMI filter_index of Shape Key Multiplier CS assigned
    if cs == 3381.4444
        ; It looks like we can skip checking for THREAD_GROUP_COUNT_Y as UAV hashes are unique for each object
        ; Ensures that dispatch_y of Shape Key Multiplier CS call matches one from dump
        ; if THREAD_GROUP_COUNT_Y == shapekeys.multiplier_dispatch_y
        ; Skip handling of original Shape Key Multiplier CS call to modify dispatch_y value
        handling = skip
        ; Run custom Shape Key Multiplier CS to apply dynamic per-character multipliers
        run = CommandListMultiplyShapeKeys
        ; endif
    endif
endif

; Resources: Shape Keys Override -------------------------

; Stores dynamically calculated CB required to override original Shape Keys CS call
[ResourceShapeKeyCBRW]
; Contains 128+128+8 values:
; * 128 uint: Shape Key offsets (continuous lists of vertex offsets)
; * 128 unorm: Shape Key values (range [0.0, 1.0])
; * 8 uint: Shape Key CS settings
type = RWBuffer
format = R32G32B32A32_UINT
; 32 shapekey offsets, 32 shapekey values, 2 control flags
array = 66

; Stores values of custom Shape Keys and overrides for original ones
[ResourceCustomShapeKeyValuesRW]
; Contains 128 values, zero is shifted by 1.0 to the right
; Expected value range is [1.0, 2.0]
; * `0.0` means `no override`
; * `1.0` means `override with zero`
; * `2.0` means `override with one`
type = RWBuffer
format = R32G32B32A32_FLOAT
; 32 elements, 4 floats per element
array = 32

; Resources: Buffers -------------------------

; Stores pointer to original vb0
[ResourceBypassVB0]

[ResourceIndexBuffer]
type = Buffer
format = DXGI_FORMAT_R32_UINT
stride = 12
filename = Meshes/Index.buf

[ResourcePositionBuffer]
type = Buffer
format = DXGI_FORMAT_R32G32B32_FLOAT
stride = 12
filename = Meshes/Position.buf

[ResourceBlendBuffer]
type = Buffer
format = DXGI_FORMAT_R8_UINT
stride = 8
filename = Meshes/Blend.buf

[ResourceVectorBuffer]
type = Buffer
format = DXGI_FORMAT_R8G8B8A8_SNORM
stride = 8
filename = Meshes/Vector.buf

[ResourceColorBuffer]
type = Buffer
format = DXGI_FORMAT_R8G8B8A8_UNORM
stride = 4
filename = Meshes/Color.buf

[ResourceTexCoordBuffer]
type = Buffer
format = DXGI_FORMAT_R16G16_FLOAT
stride = 16
filename = Meshes/TexCoord.buf

[ResourceShapeKeyOffsetBuffer]
type = Buffer
format = DXGI_FORMAT_R32G32B32A32_UINT
stride = 16
filename = Meshes/ShapeKeyOffset.buf

[ResourceShapeKeyVertexIdBuffer]
type = Buffer
format = DXGI_FORMAT_R32_UINT
stride = 4
filename = Meshes/ShapeKeyVertexId.buf

[ResourceShapeKeyVertexOffsetBuffer]
type = Buffer
format = DXGI_FORMAT_R16_FLOAT
stride = 2
filename = Meshes/ShapeKeyVertexOffset.buf

; Autogenerated -------------------------

; XXMI Launcher GameBanana: https://gamebanana.com/tools/17252
; XXMI Launcher GitHub: https://github.com/SpectrumQT/XXMI-Launcher
; WWMI Package GitHub: https://github.com/SpectrumQT/WWMI-Package
; WWMI Tools GameBanana: https://gamebanana.com/tools/17289
; WWMI Tools GitHub: https://github.com/SpectrumQT/WWMI_Tools
; AGMG Modding Community Discord: https://discord.com/invite/agmg
; SHA256 CHECKSUM: 994e77b9de92d8c101436ebb12d318ce9a970761fd2f808505647f45a9040519
