UKADOC Project
FMO/Mutex

Reference: Memory Objects (external site)

At present, this specification depends heavily on Windows-specific behavior and is not intended for implementation on other operating systems.
Shared memory and Mutexes exist on other operating systems as well, so it would be preferable to define a separate specification for them.
If a specification is established, we plan to publish it separately. Please contact the Ukadoc Project.

Mutex

To indicate that the baseware is running, SSP holds a Mutex named "ssp", while Materia and CROW hold one named "sakura".
You can determine whether they are running at low cost by checking whether this named Mutex exists.
The state of the Mutex itself is unspecified, so there is no need to check whether it is signaled.

Code example that only checks for existence (C++)

HANDLE hmutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"ssp");
if ( hmutex ) {
  // いる
  CloseHandle(hmutex);
}
else {
  //いない
}

FMO (File Mapping Object)

All baseware hold a named file mapping object (FMO) while running.
Reading this interprocess shared memory allows you to obtain a list of running ghosts at relatively low cost.
To avoid reading incomplete information while it is being written, access is controlled by the FMO Mutex. Please check this as well.

FMO name and character encoding
Sakura: OS-dependent; Shift JIS on Japanese OSes
SakuraUnicode: UTF-8 fixed [SSP 2.5.26 and later]
FMO size
The first 4 bytes (bytes 0–3) indicate the size allocated for the FMO.
This is a fixed value indicating the size allocated for the FMO itself, not the length of the information written to it.
Currently, the value is 0x00010000 in little-endian, meaning a fixed size of 64 KB.
To ensure compatibility with other programs, changing the size is not currently supported.
Data body
The data body of the FMO begins at byte 4. It uses the following format.
(32-byte identifier).(key name)[\1]value[\r\n]
[\1] is the byte value 1, and [\r\n] is CR+LF (newline).
This format is repeated over multiple lines.
End of data
The end of the data is marked by the byte value 0, as in a C string.
Therefore, the maximum usable size of the data body is 65531 bytes (65536 - 4 - 1).
If you need to write to the FMO and the size limit may be exceeded, take care to avoid writing incomplete information.
If the size limit may be exceeded, it is preferable not to write an entire data set; if that is not possible, make sure not to end in the middle of a line.

Data body example

ssp_fmo_header_00004468_000f0dea.path[\1]D:\ssp\
ssp_fmo_header_00004468_000f0dea.hwnd[\1]986602
ssp_fmo_header_00004468_000f0dea.name[\1]ラーシェ
ssp_fmo_header_00004468_000f0dea.keroname[\1]ティセ
ssp_fmo_header_00004468_000f0dea.sakura.surface[\1]0
ssp_fmo_header_00004468_000f0dea.kero.surface[\1]10
ssp_fmo_header_00004468_000f0dea.kerohwnd[\1]1052114
ssp_fmo_header_00004468_000f0dea.hwndlist[\1]986602,1052114
ssp_fmo_header_00004468_000f0dea.ghostpath[\1]D:\ssp\ghost\DE10_3001\
ssp_fmo_header_00004468_00120da6.path[\1]D:\ssp\
ssp_fmo_header_00004468_00120da6.hwnd[\1]1183142
ssp_fmo_header_00004468_00120da6.name[\1]Emily
ssp_fmo_header_00004468_00120da6.keroname[\1]Teddy
ssp_fmo_header_00004468_00120da6.sakura.surface[\1]20
ssp_fmo_header_00004468_00120da6.kero.surface[\1]10
ssp_fmo_header_00004468_00120da6.kerohwnd[\1]1117626
ssp_fmo_header_00004468_00120da6.hwndlist[\1]1183142,1117626,921002,2035340,658950
ssp_fmo_header_00004468_00120da6.ghostpath[\1]D:\ssp\ghost\emily4\
32-byte identifier
A unique identifier for one ghost instance. You must choose a string that is not duplicated elsewhere in the FMO, at minimum.
Common approaches include combining unique information and taking an MD5 hash, or combining it with an HWND (window handle).
The Materia specification does not define its length, but it should be a fixed length of 32 bytes for compatibility.
Key name and value

A key indicating the type of information and the information itself, as follows.

path
Full path to the root folder of the running baseware.
hwnd
Window handle of the main window. Decimal notation.
name
The same as sakura.name in descript.txt.
keroname
The same as kero.name in descript.txt.
sakura.surface
Surface ID currently displayed on the \0 side. Decimal notation.
kero.surface
Surface ID currently displayed on the \1 side. Decimal notation.
kerohwnd
Window handle of the \1-side window. Decimal notation.[SSP only]
hwndlist
A comma-separated list of all window handles currently in use. Decimal notation.[SSP only]
ghostpath
Full path of the running ghost.[SSP only]
fullname
The name entry in descript.txt of the running ghost.[SSP 2.5.58 and later]
modulestate
A comma-separated string indicating the state of the modules loaded by the running ghost.[SSP 2.5.79 and later]
shiori:running SHIORI is loaded (if it is not loaded, the string itself is absent)
makoto-ghost:running The ghost's MAKOTO is loaded
makoto-shell:running The shell's MAKOTO is loaded
compatible:running Compatibility mode is enabled and script.txt, etc. are loaded
If there is a communication error between SSP and its modules, critical is used instead of running
Example: ssp_fmo_header_00004468_00120da6.modulestate[\1]shiori:running,makoto-ghost:running\r\n

Mutex for FMO

FMO itself has no mechanism for exclusive control, so a separate Mutex is held to prevent read/write conflicts.
Its name is the FMO name + "FMO", for example:
FMO = Sakura : Mutex = SakuraFMO
FMO = SakuraUnicode : Mutex = SakuraUnicodeFMO
It is important to determine whether the Mutex is in a signaled or non-signaled state.
When reading or writing, always acquire ownership using WaitForSingleObject or an equivalent wait function, then release ownership with ReleaseMutex when finished.

Some older baseware may not support the Mutex for FMO, so failure to acquire the Mutex should not be treated as an error.
In this case, however, consider the possibility of obtaining an FMO during an incomplete write, and take care to write code that processes it as safely as possible.

Example code for reading and writing FMO (C++)

//ベースウェア等の保持すべきアプリは代わりにCreateMutexを使う
HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"SakuraFMO");

//FMO用Mutexに対応していないベースウェアもあるので、見つからなかったら単にスキップ
bool isWaitSuccess = true;

if ( hMutex ) {

    //INFINITEで待機すると永遠に待ち続けてGUIが止まるので適宜工夫すること
    DWORD result = WaitForSingleObject(hMutex,INFINITE);

    if ( result != WAIT_OBJECT_0 ) {
        isWaitSuccess = false;
    }
}

if ( isWaitSuccess ) {

    //保持すべきアプリは代わりにCreateMutexを使う
    HANDLE hFMO = OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"Sakura");

    if ( hFMO ) {

        char *pDataStart = static_cast<char*>(MapViewOfFile(hFMO,FILE_MAP_ALL_ACCESS,0,0,0));

        if ( pDataStart ) {

            //頭の4バイトはFMO最大サイズ。
            //文字列終端(C言語文字列のゼロ終端)とは異なるので注意。
            unsigned long length = *reinterpret_cast<unsigned long*>(pDataStart);

            char *pData = pDataStart;
            pData += 4;

            //****************************************
            //ここでpDataとlengthをつかってなにかやる
            //****************************************

            //MapViewOfFileの解除
            UnmapViewOfFile(pDataStart);
        }

        //FMOハンドルを開放
        //ベースウェア等の保持すべきアプリは開放せず持ち続けること
        CloseHandle(hFMO);
    }
}

if ( hMutex ) {
    if ( isWaitSuccess ) {

        //WaitForSingleObjectでMutexが非シグナル状態になるので、シグナル状態に戻す(Release)
        ReleaseMutex(hMutex);
    }

    //最後にMutexのハンドルも要らないので開放
    //ベースウェア等の保持すべきアプリは開放せず持ち続けること
    CloseHandle(hMutex);
}