xmod文件信息的显示

前言

从网上找到了一段显示.xmod文件信息的代码, 整理了一下.

demo

// xmodFileInfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include 
#include 

#include "xmod.h"

int main(int argc, char* argv[])
{
    if (argc != 2) {
#ifdef _DEBUG
        show_xmod_info("wmode.xmod");
#else
        printf("Usage: xmodFileInfo \r\n");
#endif
    } else {
        show_xmod_info(argv[1]);
    }

    system("pause");

    /** run result
Load mode: Startup
Info blocks in wmode.xmod:

---------------------------------
Plugin name:
W-MODE

---------------------------------
Version:
1.01

---------------------------------
Description:
Windowed mode plugin for all versions of Original StarCraft and BroodWar

---------------------------------
Author:
XeNotRoN

---------------------------------
Contact:
xenomails|at|freemail|dot|hu

---------------------------------
Build time:
September 20, 2007

---------------------------------
Company:
BWProgrammers

---------------------------------
URL:
http://www.bwprogrammers.com

---------------------------------
Usage:
Use a loader that is capable of XMOD loading.
You can use for example scloader2b to load the plug with the -xmod parameter:

scloader2b -xmod wmode.xmod

The plugin can be placed anywhere but the wmode.ini configuration file will alwa
ys be searched in your StarCraft directory. You can set the options of W-MODE in
 that file.

A sample wmode.ini file with comments and default settings:
(Default value applies when the key is not present in the config file.)

[W-MODE]

; What settings do you want to save when you exit StarCraft?

; Save WindowClientX?
; Default value: 1
SaveWindowClientX=1
; Save WindowClientY?
; Default value: 1
SaveWindowClientY=1
; Save ClipCursor?
; Default value: 0
SaveClipCursor=0
; Save EnableWindowMove?
; Default value: 1
SaveEnableWindowMove=1
; Save AlwaysOnTop?
; Default value: 1
SaveAlwaysOnTop=1
; Save DisableControls?
; Default value: 1
SaveDisableControls=1

; X and Y coordinates of the StarCraft game screen.
; (Upper left corner of client area.)
; Default values: center the game screen on the desktop.
; If you don't specify the WindowClientX value then
; the window will be centered horizontally.
; Omitting WindowClientY will cause the window to
; be centered vertically.
WindowClientX=30
WindowClientY=30

; Cursor clip (Toggle hotkey: ALT+F1)
; Default value: 0
ClipCursor=0
; Enable window move (Toggle hotkey: ALT+F10)
; Default value: 1
EnableWindowMove=1
; Enable always-on-top mode (Toggle hotkey: ALT+F11)
; Default value: 0
AlwaysOnTop=0
; Disable all controls in the caption of the StarCraft
; window. Disable the screensaver when the sc window is
; active. Window can not be closed with ALT+F4.
; (Toggle hotkey: ALT+F12)
; Default value: 0
DisableControls=0

; Limit the maximum frame/sec to reach better performance.
; This is extremely useful because during replays with
; fastest x 4 speed the frame rate raises to the skies and
; MaxFps limits to 100 the number of blits that require
; 8bit -> desktop resolution conversion of the StarCraft
; screen image.
; Default and recommended value: 100
; You can set it to higher value on faster machines.
; Minimum value: 30 (less than 100 isn't recommended)
MaxFps=100

; Enables StarCraft to mute all sound when the main window
; loses focus.
; Default value: 0
MuteNotFocused=0


---------------------------------
Language:
Neutral

---------------------------------
Original filename:
wmode.xmod
请按任意键继续. . .
    */

    return 0;
}

头文件

// xmod.h: interface for the xmod class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_XMOD_H__F46C033E_9B4F_40A9_8635_A40B84754F65__INCLUDED_)
#define AFX_XMOD_H__F46C033E_9B4F_40A9_8635_A40B84754F65__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include 

#pragma pack (push, 1)

#ifdef __cplusplus
extern "C" {
#endif

    /*
    // Value of XMOD_HEADER.Sign
    */

#define XMOD_HEADER_SIGN                0x444F4D58

    /*
    // XMOD_HEADER.Flags:
    // If the XMOD_HEADER_FLAG_STARTUPINFO flag is no set then the 
    // user can choose between startup/nonstartup load modes, but
    // if it is set then the XMOD_HEADER_FLAG_STARTUP flag determines
    // the correct load mode overriding the users choice.
    */

#define XMOD_HEADER_FLAG_STARTUPINFO    0x01
#define XMOD_HEADER_FLAG_STARTUP        0x02

typedef struct _XMOD_HEADER {
    DWORD   Sign;               /* "XMOD": XMOD_HEADER_SIGN */
    BYTE    Version;            /* A single digit XMOD version number. Zero for current version */
    BYTE    Flags;              /* XMOD_HEADER_FLAG_XXX constants */
    DWORD   Secret1;            /* Guess :) */
    DWORD   Secret2;            /* ;D */
    DWORD   SizeInfo;           /* The size of the info block that follows this header. */
} XMOD_HEADER;

    /*
    // Structure of the info block:
    // The info block consists of variable length sub blocks. All of
    // these block has an XMOD_INFO_HEADER that is followed by variable
    // length data. If the data contains text then DOS/Windows type
    // newlines must be used (CR/LF: 13,10). Newlines will be used only
    // when they are necessary and the text may contain a full big
    // paragraph without a single newline character, so you should
    // display the text with auto-wordbreak.
    */

typedef struct _XMOD_INFO_HEADER {
    DWORD   ID;                 /* One of the XMOD_INFO_XXX constants */
    DWORD   Size;               /* The size of the data that follows this header. */
} XMOD_INFO_HEADER;

    /*
    // XMOD_INFO_HEADER.ID
    //
    // XMOD_INFO_NAME: Name of the plugin.
    // XMOD_INFO_VERSION: Plugin version string.
    // XMOD_INFO_DESCRIPTION: Brief description of the plugin.
    // XMOD_INFO_AUTHOR: Author of the plugin.
    // XMOD_INFO_CONTACT: How to contact the author.
    // XMOD_INFO_TIME: Build tme of the plugin.
    // XMOD_INFO_COMPANY: Company.
    // XMOD_INFO_URL: Webpage of the company/plugin.
    // XMOD_INFO_COPYRIGHT: Copyrigth string.
    // XMOD_INFO_USAGE: Long description/usage of the plugin.
    // XMOD_INFO_LANGUAGE: Language of the plugin.
    */

#define XMOD_INFO_NAME              0x454D414E
#define XMOD_INFO_VERSION           0x20524556
#define XMOD_INFO_DESCRIPTION       0x43534544
#define XMOD_INFO_AUTHOR            0x48545541
#define XMOD_INFO_CONTACT           0x544E4F43
#define XMOD_INFO_TIME              0x454D4954
#define XMOD_INFO_COMPANY           0x504D4F43
#define XMOD_INFO_URL               0x204C5255
#define XMOD_INFO_COPYRIGHT         0x59504F43
#define XMOD_INFO_USAGE             0x20455355
#define XMOD_INFO_LANGUAGE          0x474E414C
#define XMOD_INFO_ORIGFILENAME      0x454C4946

#ifdef __cplusplus
}
#endif

#pragma pack (pop)

int show_xmod_info(char *pcXmodFilePathName);

#endif // !defined(AFX_XMOD_H__F46C033E_9B4F_40A9_8635_A40B84754F65__INCLUDED_)

实现文件

// xmod.cpp: implementation of the xmod class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include 
#include 
#include "xmod.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

FILE    *f    = NULL;
char    *data = NULL;

struct {
    DWORD   dwID;
    char    *lpszIDName;
} IDNames[] =
{
    { XMOD_INFO_NAME,           "Plugin name" },
    { XMOD_INFO_VERSION,        "Version" },
    { XMOD_INFO_DESCRIPTION,    "Description" },
    { XMOD_INFO_AUTHOR,         "Author" },
    { XMOD_INFO_CONTACT,        "Contact" },
    { XMOD_INFO_TIME,           "Build time" },
    { XMOD_INFO_COMPANY,        "Company" },
    { XMOD_INFO_URL,            "URL" },
    { XMOD_INFO_COPYRIGHT,      "Copyright" },
    { XMOD_INFO_USAGE,          "Usage" },
    { XMOD_INFO_LANGUAGE,       "Language" },
    { XMOD_INFO_ORIGFILENAME,   "Original filename" },
    { 0 }
};


void ShowMsgAndSafeClean(char *msg)
{
    if (msg)
    {
        printf (msg);
        printf ("\n");
    }

    if (f) fclose (f);
    if (data) free (data);
}

int show_xmod_info(char *pcXmodFilePathName)
{
    XMOD_HEADER         header;
    XMOD_INFO_HEADER    *pinfo;
    DWORD               dwConsumed = 0;
    int                 i;
    char                *print_buff;

    if (NULL == pcXmodFilePathName) {
        ShowMsgAndSafeClean ("Error invalid XMOD file name!");
        return S_FALSE;
    }

    if (!(f = fopen (pcXmodFilePathName, "rb"))) {
        ShowMsgAndSafeClean ("Error opeining XMOD file!");
        return S_FALSE;
    }

    if (fread (&header, 1, sizeof (header), f) != sizeof (header)) {
        ShowMsgAndSafeClean ("Error reading XMOD header!");
        return S_FALSE;
    }

    if (header.Sign != XMOD_HEADER_SIGN ||
        header.Version != 0)
    {
        ShowMsgAndSafeClean ("Invalid XMOD file!");
        return S_FALSE;
    }

    printf ("Load mode: ");

    if (header.Flags & XMOD_HEADER_FLAG_STARTUPINFO)
    {
        if (header.Flags & XMOD_HEADER_FLAG_STARTUP)
            printf ("Startup\n");
        else
            printf ("Nonstartup\n");
    }
    else
    {
        printf ("User defined\n");
    }

    if (!header.SizeInfo) {
        ShowMsgAndSafeClean ("There is no info in this XMOD.");
        return S_FALSE;
    }

    if (!(data = (char*)malloc (header.SizeInfo))) {
        ShowMsgAndSafeClean ("Out of memory.");
        return S_FALSE;
    }

    if (fread (data, 1, header.SizeInfo, f) != header.SizeInfo) {
        ShowMsgAndSafeClean ("Error reading info from the XMOD!");
        return S_FALSE;
    }

    pinfo = (XMOD_INFO_HEADER*)data;

    printf ("Info blocks in %s:\n", pcXmodFilePathName);

    for (;;)
    {
        dwConsumed += sizeof (XMOD_INFO_HEADER);
        if (dwConsumed > header.SizeInfo) {
            ShowMsgAndSafeClean (NULL);
            return S_FALSE;
        }

        dwConsumed += pinfo->Size;
        if (dwConsumed > header.SizeInfo) {
            ShowMsgAndSafeClean (NULL);
            return S_FALSE;
        }

        for (i=0; IDNames[i].lpszIDName; i++)
            if (pinfo->ID == IDNames[i].dwID) break;

        printf ("\n---------------------------------\n");

        if (IDNames[i].lpszIDName)
        {
            printf ("%s:\n", IDNames[i].lpszIDName);
            if (!(print_buff = (char*)malloc (pinfo->Size + 1))) {
                ShowMsgAndSafeClean ("Out of memory.");
                return S_FALSE;
            }
            memcpy (print_buff, pinfo + 1, pinfo->Size);
            print_buff[pinfo->Size] = 0;
            printf ("%s\n", print_buff);
            free (print_buff);
        }
        else
        {
            printf ("Unknown info block. (ID: %#8.8x)\n", pinfo->ID);
        }

        pinfo = (XMOD_INFO_HEADER*)((char*)pinfo + sizeof (XMOD_INFO_HEADER) + pinfo->Size);
    }

    return 0;
}

你可能感兴趣的:(file)