├── .gitignore ├── README.md ├── util.h ├── parts.cpp ├── dxt.h ├── iwi.h ├── surface.cpp ├── bone_offset_table.h ├── types.h ├── model.cpp ├── animation.cpp ├── main.cpp └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.suo 2 | *.db 3 | *.ipch 4 | *.pdb 5 | *.tlog 6 | *.obj 7 | *.exe 8 | Debug/ 9 | Release/ 10 | .vs/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xmodelconverter 2 | A converter for xmodel & xanim files for Call of Duty. 3 | 4 | It converts xmodel & xanim files back into xmodel_export and xanim_export text readable files. 5 | These _export files can then be more easily read and imported into 3d modelling software such as Blender (https://blender.org). 6 | 7 | ## Supported games 8 | - ~~Call of Duty~~ (not available at this moment) 9 | - Call of Duty 2 10 | 11 | ## Usage 12 | 13 | Create a new folder in your main folder, so that your structure looks like. 14 | 15 | ``` 16 | \exported 17 | \xanim 18 | \xmodel 19 | \xmodelparts 20 | \xmodelsurfs 21 | ``` 22 | 23 | **Then you can just drop and drop the xmodel or xanim file(s) onto the executable.** 24 | 25 | or use 26 | 27 | ``` 28 | xmodelconverter.exe "C:\path\to\your\xmodel\or\xanim\file" 29 | ``` 30 | 31 | **The files will be placed in the exported folder.** 32 | 33 | ## Dependencies 34 | 35 | https://github.com/g-truc/glm 36 | 37 | ## Building 38 | 39 | ``` 40 | sudo apt install libglm-dev 41 | git clone https://github.com/riicchhaarrd/xmodelconverter 42 | g++ -w *.cpp -o xmodelconverter 43 | ``` 44 | 45 | ## Blender 46 | To import the _export files into Blender, you need to install a addon. 47 | 48 | https://github.com/riicchhaarrd/io_scene_xmodel 49 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "types.h" 4 | 5 | #ifdef _WIN32 6 | #define WIN32_LEAN_AND_MEAN 7 | #include 8 | #endif 9 | 10 | #ifndef _WIN32 11 | static int fopen_s(FILE** fp, const char* filename, const char* mode) 12 | { 13 | *fp = fopen(filename, mode); 14 | return 0; 15 | } 16 | #endif 17 | 18 | namespace util 19 | { 20 | static std::vector read_file_to_memory(const std::string& _path) 21 | { 22 | std::ifstream in(_path, std::ios::binary | std::ios::ate); 23 | std::vector v; 24 | if (!in.is_open()) 25 | return v; 26 | size_t size = in.tellg(); 27 | in.seekg(0, std::ios::beg); 28 | v.resize(size); 29 | in.read((char*)v.data(), size); 30 | in.close(); 31 | return v; 32 | } 33 | 34 | static Transform get_world_transform(std::vector& bones, int index) 35 | { 36 | auto& bone = bones[index]; 37 | if (bone.parent == -1) 38 | { 39 | return Transform(bone.transform.rotation, bone.transform.translation); 40 | } 41 | auto transform = get_world_transform(bones, bone.parent); 42 | glm::quat rot = transform.rotation * bone.transform.rotation; 43 | glm::vec3 trans = glm::rotate(transform.rotation, bone.transform.translation) + transform.translation; 44 | return Transform(rot, trans); 45 | } 46 | 47 | static bool directory_exists(const std::string& szPath) 48 | { 49 | #ifdef _WIN32 50 | DWORD dwAttrib = GetFileAttributesA(szPath.c_str()); 51 | 52 | return (dwAttrib != INVALID_FILE_ATTRIBUTES && 53 | (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); 54 | #else 55 | return false; 56 | #endif 57 | } 58 | 59 | static vec3 get_translation_component_from_matrix(const mat4& mat) 60 | { 61 | vec3 v; 62 | v.x = mat[3][0]; 63 | v.y = mat[3][1]; 64 | v.z = mat[3][2]; 65 | return v; 66 | } 67 | 68 | static void get_xyz_components_from_matrix(const mat4& mat, vec3& rx, vec3& ry, vec3& rz) 69 | { 70 | rx.x = mat[0][0]; 71 | rx.y = mat[0][1]; 72 | rx.z = mat[0][2]; 73 | ry.x = mat[1][0]; 74 | ry.y = mat[1][1]; 75 | ry.z = mat[1][2]; 76 | rz.x = mat[2][0]; 77 | rz.y = mat[2][1]; 78 | rz.z = mat[2][2]; 79 | } 80 | }; -------------------------------------------------------------------------------- /parts.cpp: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "util.h" 3 | #include "bone_offset_table.h" 4 | 5 | bool XModelParts::read_xmodelparts_file(XModel &xm, BinaryReader &rd) 6 | { 7 | u16 version = rd.read(); 8 | if (version != 0x14) 9 | return rd.set_error_message("expected xmodelparts version 0x14, got %x\n", version); 10 | 11 | this->numbonesrelative = rd.read(); 12 | this->numbonesabsolute = rd.read(); 13 | this->numbonestotal = this->numbonesrelative + this->numbonesabsolute; 14 | 15 | printf("version = %d\n", version); 16 | printf("numbonestotal = %d\n", this->numbonestotal); 17 | 18 | this->bones.resize(this->numbonestotal); 19 | 20 | for (int i = 0; i < this->numbonesrelative; ++i) 21 | { 22 | int parent = rd.read(); 23 | vec3 trans = rd.read(); 24 | quat rot = rd.read_quat(); 25 | 26 | this->bones[i + numbonesabsolute].transform.rotation = rot; 27 | this->bones[i + numbonesabsolute].transform.translation = trans; 28 | this->bones[i + numbonesabsolute].parent = parent; 29 | } 30 | 31 | for (int j = 0; j < this->numbonestotal; ++j) 32 | { 33 | auto& bone = this->bones[j]; 34 | std::string bonename; 35 | u8 c; 36 | while ((c = rd.read())) 37 | bonename.push_back(c); 38 | if (bonename.empty()) 39 | break; 40 | this->bones[j].name = bonename; 41 | this->bonemap[bonename] = j; 42 | if (xm.viewhands && j > 0) 43 | { 44 | for (int z = 0; viewmodel_offsets_table[z].bonename; ++z) 45 | { 46 | if (!strcmp(viewmodel_offsets_table[z].bonename, bonename.c_str())) 47 | { 48 | //printf("replacing bone '%s' offset with %f,%f,%f\n", bone.c_str(), viewmodel_offsets_table[z].offset.x, viewmodel_offsets_table[z].offset.y, viewmodel_offsets_table[z].offset.z); 49 | bone.transform.translation = viewmodel_offsets_table[z].offset / 2.54f; 50 | break; 51 | } 52 | } 53 | } 54 | //printf("\bone %d: %s\n", j, bone.c_str()); 55 | } 56 | 57 | //read partclassification 58 | 59 | for (int i = 0; i < this->numbonestotal; ++i) 60 | { 61 | u8 b = rd.read(); 62 | //printf("partclassification %d: %d\n", i, b & 0xff); 63 | } 64 | printf("%d/%d\n", rd.m_pos, rd.m_buf.size()); 65 | return true; 66 | } 67 | -------------------------------------------------------------------------------- /dxt.h: -------------------------------------------------------------------------------- 1 | /* 2 | Jonathan Dummer 3 | 2007-07-31-10.32 4 | 5 | simple DXT compression / decompression code 6 | 7 | public domain 8 | */ 9 | 10 | #ifndef HEADER_IMAGE_DXT 11 | #define HEADER_IMAGE_DXT 12 | 13 | /** A bunch of DirectDraw Surface structures and flags **/ 14 | typedef struct 15 | { 16 | unsigned int dwMagic; 17 | unsigned int dwSize; 18 | unsigned int dwFlags; 19 | unsigned int dwHeight; 20 | unsigned int dwWidth; 21 | unsigned int dwPitchOrLinearSize; 22 | unsigned int dwDepth; 23 | unsigned int dwMipMapCount; 24 | unsigned int dwReserved1[11]; 25 | 26 | /* DDPIXELFORMAT */ 27 | struct 28 | { 29 | unsigned int dwSize; 30 | unsigned int dwFlags; 31 | unsigned int dwFourCC; 32 | unsigned int dwRGBBitCount; 33 | unsigned int dwRBitMask; 34 | unsigned int dwGBitMask; 35 | unsigned int dwBBitMask; 36 | unsigned int dwAlphaBitMask; 37 | } 38 | sPixelFormat; 39 | 40 | /* DDCAPS2 */ 41 | struct 42 | { 43 | unsigned int dwCaps1; 44 | unsigned int dwCaps2; 45 | unsigned int dwDDSX; 46 | unsigned int dwReserved; 47 | } 48 | sCaps; 49 | unsigned int dwReserved2; 50 | } 51 | DDS_header; 52 | 53 | /* the following constants were copied directly off the MSDN website */ 54 | 55 | /* The dwFlags member of the original DDSURFACEDESC2 structure 56 | can be set to one or more of the following values. */ 57 | #define DDSD_CAPS 0x00000001 58 | #define DDSD_HEIGHT 0x00000002 59 | #define DDSD_WIDTH 0x00000004 60 | #define DDSD_PITCH 0x00000008 61 | #define DDSD_PIXELFORMAT 0x00001000 62 | #define DDSD_MIPMAPCOUNT 0x00020000 63 | #define DDSD_LINEARSIZE 0x00080000 64 | #define DDSD_DEPTH 0x00800000 65 | 66 | /* DirectDraw Pixel Format */ 67 | #define DDPF_ALPHAPIXELS 0x00000001 68 | #define DDPF_FOURCC 0x00000004 69 | #define DDPF_RGB 0x00000040 70 | 71 | /* The dwCaps1 member of the DDSCAPS2 structure can be 72 | set to one or more of the following values. */ 73 | #define DDSCAPS_COMPLEX 0x00000008 74 | #define DDSCAPS_TEXTURE 0x00001000 75 | #define DDSCAPS_MIPMAP 0x00400000 76 | 77 | /* The dwCaps2 member of the DDSCAPS2 structure can be 78 | set to one or more of the following values. */ 79 | #define DDSCAPS2_CUBEMAP 0x00000200 80 | #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400 81 | #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800 82 | #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000 83 | #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000 84 | #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000 85 | #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000 86 | #define DDSCAPS2_VOLUME 0x00200000 87 | 88 | #endif /* HEADER_IMAGE_DXT */ 89 | -------------------------------------------------------------------------------- /iwi.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "types.h" 4 | #include "dxt.h" 5 | 6 | struct IWI 7 | { 8 | enum class USAGE_TYPE 9 | { 10 | INVALID = -1, 11 | COLOR, 12 | DEFAULT, 13 | SKYBOX = 0x5, 14 | DECAL_BEG = 9, 15 | DECAL_END = 19 16 | }; 17 | 18 | enum class FORMAT_TYPE 19 | { 20 | INVALID = -1, 21 | ARGB32, 22 | RGB24, 23 | GA16, 24 | A8, 25 | DXT1 = 0xb, 26 | DXT3, 27 | DXT5 28 | }; 29 | 30 | unsigned short width, height; 31 | USAGE_TYPE usage; 32 | FORMAT_TYPE format; 33 | 34 | u8* texture_data_ptr; 35 | size_t texture_data_sz; 36 | 37 | IWI() : usage(USAGE_TYPE::INVALID), format(FORMAT_TYPE::INVALID), width(0), height(0), texture_data_ptr(0), texture_data_sz(0) {} 38 | 39 | bool read_from_memory(BinaryReader &rd) 40 | { 41 | u8 hdr[4]; 42 | for (int i = 0; i < 3; i++) 43 | hdr[i] = rd.read(); 44 | hdr[3] = rd.read() + '0'; 45 | 46 | if (hdr[0] != 'I' || hdr[1] != 'W' || hdr[2] != 'i' || hdr[3] != '5') 47 | { 48 | rd.set_error_message("unsupported IWI file '%c%c%c%c'\n", hdr[0], hdr[1], hdr[2], hdr[3]); 49 | return false; 50 | } 51 | 52 | format = (FORMAT_TYPE)rd.read(); 53 | usage = (USAGE_TYPE)rd.read(); 54 | 55 | width = rd.read(); 56 | height = rd.read(); 57 | 58 | rd.skip(2); 59 | 60 | u32 filesize = rd.read(); 61 | u32 texture_offset = rd.read(); 62 | texture_data_ptr = rd.buffer() + texture_offset; 63 | texture_data_sz = filesize - texture_offset; 64 | //printf("width=%d,height=%d,format=%d,usage=%d,texture_data_sz=%d\n", width, height, format, usage, texture_data_sz); 65 | //getchar(); 66 | return true; 67 | } 68 | 69 | bool build_dds(std::vector &v) const 70 | { 71 | v.resize(sizeof(DDS_header)); 72 | 73 | DDS_header* hdr = (DDS_header*)v.data(); 74 | 75 | memset(hdr, 0, sizeof(DDS_header)); 76 | hdr->dwMagic = ('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24); 77 | hdr->dwSize = 124; 78 | hdr->dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_LINEARSIZE; 79 | hdr->dwWidth = width; 80 | hdr->dwHeight = height; 81 | hdr->dwPitchOrLinearSize = texture_data_sz; 82 | hdr->sPixelFormat.dwSize = 32; 83 | hdr->sPixelFormat.dwFlags = DDPF_FOURCC; 84 | if (format == FORMAT_TYPE::DXT1) 85 | { 86 | hdr->sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('1' << 24); 87 | } 88 | else 89 | { 90 | hdr->sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('5' << 24); 91 | } 92 | hdr->sCaps.dwCaps1 = DDSCAPS_TEXTURE; 93 | v.insert(v.end(), texture_data_ptr, texture_data_ptr + texture_data_sz); 94 | return true; 95 | } 96 | }; -------------------------------------------------------------------------------- /surface.cpp: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "util.h" 3 | 4 | bool XModelSurface::read_xmodelsurface_file(XModelParts &parts, BinaryReader &rd) 5 | { 6 | this->clear(); 7 | u16 version = rd.read(); 8 | if (version != 0x14) 9 | return rd.set_error_message("expected xmodelsurface version 0x14, got %x\n", version); 10 | //printf("xmodelsurface version %d\n", version); 11 | 12 | //used for checking against xmodel numsurfs for file version conflict (e.g iwd/non-iwd) 13 | u16 numsurfs = rd.read(); 14 | 15 | int idx = 0; 16 | for (int i = 0; i < numsurfs; ++i) 17 | { 18 | std::vector vertices; 19 | u8 tilemode = rd.read(); 20 | u16 vertcount = rd.read(); 21 | u16 tricount = rd.read(); 22 | i16 boneoffset = rd.read(); 23 | if (boneoffset == -1) 24 | { 25 | rd.read(); 26 | } 27 | 28 | for (int j = 0; j < vertcount; ++j) 29 | { 30 | Vertex vtx; 31 | vtx.numweights = 0; 32 | vec3 n = rd.read(); 33 | n *= -1.f; 34 | u32 color = rd.read(); 35 | float u, v; 36 | u = rd.read(); 37 | v = rd.read(); 38 | vec3 binormal = rd.read(); 39 | vec3 tangent = rd.read(); 40 | 41 | u8 numweights = 0; 42 | u16 boneindex = boneoffset == -1 ? 0 : boneoffset; 43 | vec3 offset; 44 | 45 | vtx.normal = n; 46 | vtx.uv.x = u; 47 | vtx.uv.y = v; 48 | vtx.binormal = binormal; 49 | vtx.tangent = tangent; 50 | 51 | if (boneoffset == -1) 52 | { 53 | numweights = rd.read(); 54 | boneindex = rd.read(); 55 | } 56 | offset = rd.read(); 57 | vtx.numweights = numweights + 1; 58 | vtx.boneweights[0] = 1.f; 59 | vtx.boneindices[0] = boneindex; 60 | 61 | if (numweights > 0) 62 | { 63 | rd.read(); //idk weight? 64 | for (int k = 0; k < numweights; ++k) 65 | { 66 | u16 blendindex = rd.read(); 67 | vec3 blendoffset = rd.read(); 68 | float blendweight = ((float)rd.read()) / (float)USHRT_MAX; 69 | vtx.boneweights[0] -= blendweight; 70 | vtx.boneweights[k + 1] = blendweight; 71 | vtx.boneindices[k + 1] = blendindex; 72 | } 73 | } 74 | auto transform = util::get_world_transform(parts.bones, boneindex); 75 | vtx.pos = glm::rotate(transform.rotation, offset) + transform.translation; 76 | vtx.normal = glm::rotate(transform.rotation, vtx.normal); 77 | vertices.push_back(vtx); 78 | } 79 | Mesh mesh; 80 | for (int i = 0; i < tricount; ++i) 81 | { 82 | u16 face[3]; 83 | face[0] = rd.read(); 84 | face[2] = rd.read(); 85 | face[1] = rd.read(); 86 | 87 | for (int j = 0; j < 3; ++j) 88 | { 89 | this->vertices.push_back(vertices.at(face[j])); 90 | mesh.indices.push_back(idx++); 91 | } 92 | } 93 | this->meshes.push_back(mesh); 94 | } 95 | 96 | return true; 97 | } 98 | -------------------------------------------------------------------------------- /bone_offset_table.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | //Table from https://github.com/Scobalula/Greyhound/blob/development_v2/src/Greyhound/Greyhound.Logic/Helpers/XModelFileHelper.cs#L552 3 | 4 | typedef struct 5 | { 6 | const char* bonename; 7 | glm::vec3 offset; 8 | } bone_offset_table_t; 9 | 10 | static const bone_offset_table_t viewmodel_offsets_table[] = { 11 | { "tag_view", glm::vec3{0.f, 0.f, 0.f} }, 12 | { "tag_torso", glm::vec3{-11.76486f, 0.f, -3.497466f} }, 13 | { "j_shoulder_le", glm::vec3{2.859542f, 20.16072f, -4.597286f} }, 14 | { "j_elbow_le", glm::vec3{30.7185f, -8E-06f, 3E-06f} }, 15 | { "j_wrist_le", glm::vec3{29.3906f, 1.9E-05f, -3E-06f} }, 16 | { "j_thumb_le_0", glm::vec3{2.786345f, 2.245192f, 0.85161f} }, 17 | { "j_thumb_le_1", glm::vec3{4.806596f, -1E-06f, 3E-06f} }, 18 | { "j_thumb_le_2", glm::vec3{2.433519f, -2E-06f, 1E-06f} }, 19 | { "j_thumb_le_3", glm::vec3{3.f, -1E-06f, -1E-06f} }, 20 | { "j_flesh_le", glm::vec3{4.822557f, 1.176307f, -0.110341f} }, 21 | { "j_index_le_0", glm::vec3{10.53435f, 2.786251f, -3E-06f} }, 22 | { "j_index_le_1", glm::vec3{4.563f, -3E-06f, 1E-06f} }, 23 | { "j_index_le_2", glm::vec3{2.870304f, 3E-06f, -2E-06f} }, 24 | { "j_index_le_3", glm::vec3{2.999999f, 4E-06f, 1E-06f} }, 25 | { "j_mid_le_0", glm::vec3{10.71768f, 0.362385f, -0.38647f} }, 26 | { "j_mid_le_1", glm::vec3{4.842623f, -1E-06f, -1E-06f} }, 27 | { "j_mid_le_2", glm::vec3{2.957112f, -1E-06f, -1E-06f} }, 28 | { "j_mid_le_3", glm::vec3{3.000005f, 4E-06f, 0.f} }, 29 | { "j_ring_le_0", glm::vec3{9.843364f, -1.747671f, -0.401116f} }, 30 | { "j_ring_le_1", glm::vec3{4.842618f, 4E-06f, -3E-06f} }, 31 | { "j_ring_le_2", glm::vec3{2.755294f, -2E-06f, 5E-06f} }, 32 | { "j_ring_le_3", glm::vec3{2.999998f, -2E-06f, -4E-06f} }, 33 | { "j_pinky_le_0", glm::vec3{8.613766f, -3.707476f, 0.16818f} }, 34 | { "j_pinky_le_1", glm::vec3{3.942609f, 1E-06f, 1E-06f} }, 35 | { "j_pinky_le_2", glm::vec3{1.794117f, 3E-06f, -3E-06f} }, 36 | { "j_pinky_le_3", glm::vec3{2.83939f, -1E-06f, 4E-06f} }, 37 | { "j_wristtwist_le", glm::vec3{21.60379f, 1.2E-05f, -3E-06f} }, 38 | { "j_shoulder_ri", glm::vec3{2.859542f, -20.16072f, -4.597286f} }, 39 | { "j_elbow_ri", glm::vec3{-30.71852f, 4E-06f, -2.4E-05f} }, 40 | { "j_wrist_ri", glm::vec3{-29.39067f, 4.4E-05f, 2.2E-05f} }, 41 | { "j_thumb_ri_0", glm::vec3{-2.786155f, -2.245166f, -0.851634f} }, 42 | { "j_thumb_ri_1", glm::vec3{-4.806832f, -6.6E-05f, 0.000141f} }, 43 | { "j_thumb_ri_2", glm::vec3{-2.433458f, -3.8E-05f, -5.3E-05f} }, 44 | { "j_thumb_ri_3", glm::vec3{-3.000123f, 0.00016f, 2.5E-05f} }, 45 | { "j_flesh_ri", glm::vec3{-4.822577f, -1.176315f, 0.110318f} }, 46 | { "j_index_ri_0", glm::vec3{-10.53432f, -2.786281f, -7E-06f} }, 47 | { "j_index_ri_1", glm::vec3{-4.562927f, -5.8E-05f, 5.4E-05f} }, 48 | { "j_index_ri_2", glm::vec3{-2.870313f, -6.5E-05f, 0.0001f} }, 49 | { "j_index_ri_3", glm::vec3{-2.999938f, 0.000165f, -6.5E-05f} }, 50 | { "j_mid_ri_0", glm::vec3{-10.71752f, -0.362501f, 0.386463f} }, 51 | { "j_mid_ri_1", glm::vec3{-4.842728f, 0.000151f, 2.8E-05f} }, 52 | { "j_mid_ri_2", glm::vec3{-2.957152f, -8.7E-05f, -2.2E-05f} }, 53 | { "j_mid_ri_3", glm::vec3{-3.00006f, -6.8E-05f, -1.9E-05f} }, 54 | { "j_ring_ri_0", glm::vec3{-9.843175f, 1.747613f, 0.401109f} }, 55 | { "j_ring_ri_1", glm::vec3{-4.842774f, 0.000176f, -6.3E-05f} }, 56 | { "j_ring_ri_2", glm::vec3{-2.755269f, -1.1E-05f, 0.000149f} }, 57 | { "j_ring_ri_3", glm::vec3{-3.000048f, -4.1E-05f, -4.9E-05f} }, 58 | { "j_pinky_ri_0", glm::vec3{-8.613756f, 3.707438f, -0.168202f} }, 59 | { "j_pinky_ri_1", glm::vec3{-3.942537f, -0.000117f, -6.5E-05f} }, 60 | { "j_pinky_ri_2", glm::vec3{-1.794038f, 0.000134f, 0.000215f} }, 61 | { "j_pinky_ri_3", glm::vec3{-2.839375f, 5.6E-05f, -0.000115f} }, 62 | { "j_wristtwist_ri", glm::vec3{-21.60388f, 9.7E-05f, 8E-06f} }, 63 | { "tag_weapon", glm::vec3{38.5059f, 0.f, -17.15191f} }, 64 | { "tag_cambone", glm::vec3{0.f, 0.f, 0.f} }, 65 | { "tag_camera", glm::vec3{0.f, 0.f, 0.f} }, 66 | {NULL, glm::vec3{0,0,0}} 67 | }; -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | //#include //since c++20, for now let's just stick to c formatting 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define GLM_FORCE_CTOR_INIT //make sure values are zero initialized 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | using vec2 = glm::vec2; 20 | using vec3 = glm::vec3; 21 | using vec4 = glm::vec4; 22 | using mat4 = glm::mat4; 23 | using quat = glm::quat; 24 | 25 | typedef uint8_t u8; 26 | typedef uint16_t u16; 27 | typedef uint32_t u32; 28 | typedef int8_t i8; 29 | typedef int16_t i16; 30 | typedef int32_t i32; 31 | 32 | typedef enum 33 | { 34 | LOD_HIGHEST = 0, //unsure, can be either 0 or 4, couldn't compile model to test got error 35 | /* 36 | ************ ERROR ************ 37 | Can not have deforming triangles on collision LOD 38 | */ 39 | LOD_MEDIUM = 1, 40 | LOD_LOW = 2, 41 | LOD_LOWEST = 3, 42 | LOD_NONE = -1 43 | } collision_lod_t; 44 | 45 | struct BinaryReader 46 | { 47 | std::string m_error_message; 48 | std::string m_path; 49 | using buffer_t = std::vector; 50 | 51 | buffer_t m_buf; 52 | size_t m_pos; 53 | BinaryReader() 54 | : 55 | m_pos(0) 56 | { 57 | } 58 | const std::string& get_path() const { return m_path; } 59 | bool open_path(const std::string& path); 60 | 61 | template 62 | bool set_error_message(const char* fmt, Ts ... ts) 63 | { 64 | char buf[1024]; 65 | snprintf(buf, sizeof(buf), fmt, ts...); 66 | m_error_message = buf; 67 | return false; 68 | } 69 | 70 | const std::string& get_error_message() const 71 | { 72 | return m_error_message; 73 | } 74 | 75 | bool read_null_terminated_string(std::string& s) 76 | { 77 | s.clear(); 78 | u8 c; 79 | while ((c = read())) 80 | s.push_back(c); 81 | return !s.empty(); 82 | } 83 | 84 | template 85 | std::vector read_typed_buffer_to_vector(size_t N) 86 | { 87 | std::vector v; 88 | for (size_t i = 0; i < N; ++i) 89 | v.push_back(read()); 90 | return v; 91 | } 92 | 93 | void skip(size_t n) 94 | { 95 | m_pos += n; 96 | } 97 | 98 | u8* buffer() const 99 | { 100 | return (u8*)m_buf.data(); 101 | } 102 | 103 | template 104 | T read() 105 | { 106 | T _value = *(T*)(m_buf.data() + m_pos); 107 | m_pos += sizeof(T); 108 | return _value; 109 | } 110 | 111 | glm::quat read_quat(bool flipquat = false, bool simplequat = false) 112 | { 113 | float x = 0.f; 114 | float y = 0.f; 115 | float z = 0.f; 116 | float w = 0.f; 117 | 118 | if (simplequat) 119 | { 120 | z = ((float)read()) / (float)SHRT_MAX; 121 | } 122 | else 123 | { 124 | x = ((float)read()) / (float)SHRT_MAX; 125 | y = ((float)read()) / (float)SHRT_MAX; 126 | z = ((float)read()) / (float)SHRT_MAX; 127 | } 128 | 129 | w = 1.f - x * x - y * y - z * z; 130 | if (w > 0.f) 131 | w = sqrt(w); 132 | //if (flipquat) 133 | //return glm::quat(w, x, -z, y); 134 | return glm::quat(w, x, y, z); 135 | } 136 | }; 137 | 138 | struct Transform 139 | { 140 | glm::quat rotation; 141 | glm::vec3 translation; 142 | glm::vec3 scale; 143 | Transform() : scale(1.f), rotation(1.f, 0.f, 0.f, 0.f) {} 144 | Transform(const glm::quat& rot, const glm::vec3& trans) : rotation(rot), translation(trans), scale(1.f) {} 145 | 146 | glm::mat4 get_matrix() 147 | { 148 | glm::mat4 mat = glm::toMat4(rotation); 149 | mat[3][0] = translation.x; 150 | mat[3][1] = translation.y; 151 | mat[3][2] = translation.z; 152 | mat[3][3] = 1.f; 153 | return mat; 154 | } 155 | }; 156 | 157 | struct Vertex 158 | { 159 | int boneindices[4]; 160 | float boneweights[4]; 161 | int numweights; 162 | vec3 pos; 163 | vec3 normal; 164 | vec2 uv; 165 | vec3 tangent, binormal; 166 | 167 | Vertex() 168 | { 169 | for (int i = 0; i < 4; ++i) 170 | { 171 | boneindices[i] = -1; 172 | boneweights[i] = 0.0f; 173 | } 174 | numweights = 0; 175 | } 176 | }; 177 | 178 | struct Bone 179 | { 180 | std::string name; 181 | int parent; 182 | Transform transform; 183 | 184 | Bone() 185 | : 186 | parent(-1) 187 | { 188 | } 189 | }; 190 | 191 | struct XModelParts 192 | { 193 | std::vector bones; 194 | std::unordered_map bonemap; 195 | u16 numbonestotal; 196 | u16 numbonesrelative; 197 | u16 numbonesabsolute; 198 | 199 | int find_bone_index_by_name(const std::string& name) 200 | { 201 | for (int i = 0; i < bones.size(); ++i) 202 | { 203 | if (bones[i].name == name) 204 | return i; 205 | } 206 | return -1; 207 | } 208 | bool read_xmodelparts_file(struct XModel &xm, BinaryReader&); 209 | }; 210 | 211 | struct Mesh 212 | { 213 | std::vector indices; 214 | }; 215 | 216 | struct XModelSurface 217 | { 218 | std::vector vertices; 219 | std::vector meshes; 220 | void clear() 221 | { 222 | meshes.clear(); 223 | vertices.clear(); 224 | } 225 | 226 | size_t numfaces() 227 | { 228 | size_t sum = 0; 229 | for (auto& m : meshes) 230 | sum += m.indices.size() / 3; 231 | return sum; 232 | } 233 | bool read_xmodelsurface_file(XModelParts& parts, BinaryReader&); 234 | }; 235 | 236 | struct XModel 237 | { 238 | XModelParts parts; 239 | //should probably be a array/vector for all the lods 240 | XModelSurface surface; 241 | std::vector lodstrings; 242 | std::vector materials; 243 | bool viewhands; 244 | 245 | bool read_xmodel_file(BinaryReader&); 246 | bool export_file(const std::string& filename); 247 | }; 248 | 249 | struct XAnimFrame 250 | { 251 | //have to seperate these incase we do have a good rotation but no good translation or other way around 252 | std::map quats; 253 | std::map trans; 254 | }; 255 | 256 | struct XAnim 257 | { 258 | BinaryReader* m_reader; 259 | XModel* m_reference; 260 | 261 | u16 m_version; 262 | u16 m_numframes, m_numparts; 263 | u8 m_flags; 264 | u16 m_framerate; 265 | float m_frequency; 266 | 267 | std::map m_animframes; 268 | std::vector> m_refframes; 269 | 270 | void read_translations(const std::string& tag); 271 | void read_rotations(const std::string& tag, bool flipquat, bool simplequat); 272 | bool read_xanim_file(BinaryReader&); 273 | bool export_file(const std::string& filename); 274 | }; -------------------------------------------------------------------------------- /model.cpp: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "util.h" 3 | 4 | bool XModel::read_xmodel_file(BinaryReader& rd) 5 | { 6 | viewhands = rd.get_path().find("viewmodel_hands") != std::string::npos; 7 | 8 | u16 version = rd.read(); 9 | if (version != 0x14) 10 | return rd.set_error_message("expected xmodel version 0x14, got %x\n", version); 11 | u8 flags = rd.read(); 12 | vec3 mins = rd.read(); 13 | vec3 maxs = rd.read(); 14 | printf("version = %d\n", version); 15 | printf("flags = %02X\n", flags & 0xff); 16 | printf("mins = %f,%f,%f\n", mins.x, mins.y, mins.z); 17 | printf("maxs = %f,%f,%f\n", maxs.x, maxs.y, maxs.z); 18 | int numlods = 0; 19 | for (int i = 0; i < 4; ++i) 20 | { 21 | float dist = rd.read(); 22 | std::string lodfilename; 23 | u8 c; 24 | while ((c = rd.read())) 25 | lodfilename.push_back(c); 26 | if (lodfilename.empty()) 27 | continue; 28 | printf("lod %d: %s (%f)\n", i, lodfilename.c_str(), dist); 29 | ++numlods; 30 | this->lodstrings.push_back(lodfilename); 31 | } 32 | 33 | collision_lod_t collisionlod = (collision_lod_t)rd.read(); 34 | printf("collisionlod=%d\n", collisionlod); 35 | i32 numcollsurfs = rd.read(); 36 | printf("numcollsurfs = %d\n", numcollsurfs); 37 | if (numcollsurfs != -1) 38 | { 39 | for (int i = 0; i < numcollsurfs; ++i) 40 | { 41 | i32 numcolltris = rd.read(); 42 | if (numcolltris > 0) 43 | { 44 | printf("numcolltris = %d\n", numcolltris); 45 | for (int j = 0; j < numcolltris; ++j) 46 | { 47 | 48 | vec3 normal = rd.read(); 49 | float dist = rd.read(); 50 | vec4 svec = rd.read(); 51 | vec4 tvec = rd.read(); 52 | } 53 | } 54 | vec3 mins = rd.read(); 55 | vec3 maxs = rd.read(); 56 | int boneindex = rd.read(); 57 | int contents = rd.read(); 58 | int surfaceflags = rd.read(); 59 | } 60 | } 61 | u16 nummaterials = rd.read(); 62 | printf("nummaterials=%d\n", nummaterials); 63 | for (int i = 0; i < nummaterials; ++i) 64 | { 65 | std::string material; 66 | u8 c; 67 | while ((c = rd.read())) 68 | material.push_back(c); 69 | if (material.empty()) 70 | break; 71 | this->materials.push_back(material); 72 | printf("\tmaterial %s\n", material.c_str()); 73 | } 74 | 75 | //don't care about the mins maxs 76 | return true; 77 | 78 | for (int i = 0; i < parts.numbonestotal; ++i) 79 | { 80 | vec3 mins, maxs; 81 | mins = rd.read(); 82 | maxs = rd.read(); 83 | printf("bone mins & maxs\n"); 84 | printf("mins = %f,%f,%f\n", 85 | mins[0], 86 | mins[1], 87 | mins[2] 88 | ); 89 | printf("maxs = %f,%f,%f\n", 90 | maxs[0], 91 | maxs[1], 92 | maxs[2] 93 | ); 94 | 95 | vec3 offset = (mins + maxs) * 0.5f; 96 | printf("offset = %f,%f,%f\n", 97 | offset[0], 98 | offset[1], 99 | offset[2] 100 | ); 101 | } 102 | return true; 103 | } 104 | 105 | bool XModel::export_file(const std::string& filename) 106 | { 107 | FILE* fp = NULL; 108 | //fp = stdout; 109 | std::string fullfilename = filename + ".xmodel_export"; 110 | fopen_s(&fp, fullfilename.c_str(), "w"); 111 | if (!fp) 112 | return false; 113 | fprintf(fp, "// This was file generated with https://github.com/riicchhaarrd/xmodelconverter\n"); 114 | fprintf(fp, "MODEL\n"); 115 | fprintf(fp, "VERSION 6\n"); 116 | fprintf(fp, "\n"); 117 | fprintf(fp, "NUMBONES %d\n", parts.numbonestotal); 118 | 119 | int boneindex = 0; 120 | for (auto& b : parts.bones) 121 | { 122 | fprintf(fp, "BONE %d %d \"%s\"\n", boneindex++, b.parent, b.name.c_str()); 123 | } 124 | fprintf(fp, "\n"); 125 | boneindex = 0; 126 | for (auto& b : parts.bones) 127 | { 128 | vec3 x, y, z; 129 | auto mat = util::get_world_transform(parts.bones, boneindex).get_matrix(); 130 | util::get_xyz_components_from_matrix(mat, x, y, z); 131 | 132 | vec3 offset = util::get_translation_component_from_matrix(mat); 133 | fprintf(fp, "BONE %d\n", boneindex); 134 | //printf("BONE %d //bone name: %s, parent bone name: %s\n", boneindex, b.name.c_str(), b.parent==-1?"no parent":bones[b.parent].name.c_str()); 135 | //printf("OFFSET %f, %f, %f\n", b.offset.x, b.offset.y, b.offset.z); 136 | fprintf(fp, "OFFSET %f, %f, %f\n", offset.x, offset.y, offset.z); 137 | fprintf(fp, "SCALE 1.000000, 1.000000, 1.000000\n"); 138 | //printf("QUAT %f, %f, %f, %f //len=%f\n", b.q.x, b.q.y, b.q.z, b.q.w, glm::length(b.q)); 139 | fprintf(fp, "X %f, %f, %f\n", x.x, x.y, x.z); 140 | fprintf(fp, "Y %f, %f, %f\n", y.x, y.y, y.z); 141 | fprintf(fp, "Z %f, %f, %f\n", z.x, z.y, z.z); 142 | fprintf(fp, "\n"); 143 | ++boneindex; 144 | } 145 | 146 | fprintf(fp, "NUMVERTS %d\n", surface.vertices.size()); 147 | int nv = 0; 148 | for (auto& v : surface.vertices) 149 | { 150 | fprintf(fp, "VERT %d\n", nv++); 151 | fprintf(fp, "OFFSET %f, %f, %f\n", v.pos.x, v.pos.y, v.pos.z); 152 | if (v.numweights == 0) 153 | { 154 | fprintf(fp, "BONES 1\n"); 155 | //TODO: FIXME what if the first bone isn't the root bone? e.g bone with -1 parent 156 | fprintf(fp, "BONE 0 1.000000\n"); 157 | } 158 | else 159 | { 160 | fprintf(fp, "BONES %d\n", v.numweights); 161 | for (int k = 0; k < v.numweights; ++k) 162 | { 163 | fprintf(fp, "BONE %d %f\n", v.boneindices[k], v.boneweights[k]); 164 | } 165 | } 166 | fprintf(fp, "\n"); 167 | } 168 | 169 | fprintf(fp, "NUMFACES %d\n", surface.numfaces()); 170 | int meshindex = 0; 171 | for (auto& m : surface.meshes) 172 | { 173 | for (int i = 0; i < m.indices.size(); i += 3) 174 | { 175 | fprintf(fp, "TRI %d %d 0 0\n", meshindex, meshindex); 176 | auto& v1 = surface.vertices[m.indices[i]]; 177 | auto& v2 = surface.vertices[m.indices[i + 1]]; 178 | auto& v3 = surface.vertices[m.indices[i + 2]]; 179 | for (int k = 0; k < 3; ++k) 180 | { 181 | auto& kv = surface.vertices[m.indices[i + k]]; 182 | fprintf(fp, "VERT %d\n", m.indices[i + k]); 183 | fprintf(fp, "NORMAL %f %f %f\n", kv.normal.x, kv.normal.y, kv.normal.z); 184 | fprintf(fp, "COLOR 1.000000 1.000000 1.000000 1.000000\n"); 185 | fprintf(fp, "UV 1 %f %f\n", kv.uv.x, kv.uv.y); 186 | } 187 | fprintf(fp, "\n"); 188 | } 189 | ++meshindex; 190 | } 191 | meshindex = 0; 192 | fprintf(fp, "NUMOBJECTS %d\n", surface.meshes.size()); 193 | for (auto& m : surface.meshes) 194 | { 195 | fprintf(fp, "OBJECT %d \"mesh_%d\"\n", meshindex, meshindex); 196 | ++meshindex; 197 | } 198 | fprintf(fp, "\n"); 199 | fprintf(fp, "NUMMATERIALS %d\n", this->materials.size()); 200 | int matindex = 0; 201 | for (auto& mat : this->materials) 202 | { 203 | fprintf(fp, "MATERIAL %d \"%s\" \"Lambert\" \"test.jpg\"\n", matindex++, mat.c_str()); 204 | fprintf(fp, "COLOR 0.000000 0.000000 0.000000 1.000000\n"); 205 | fprintf(fp, "TRANSPARENCY 0.000000 0.000000 0.000000 1.000000\n"); 206 | fprintf(fp, "AMBIENTCOLOR 0.000000 0.000000 0.000000 1.000000\n"); 207 | fprintf(fp, "INCANDESCENCE 0.000000 0.000000 0.000000 1.000000\n"); 208 | fprintf(fp, "COEFFS 0.800000 0.000000\n"); 209 | fprintf(fp, "GLOW 0.000000 0\n"); 210 | fprintf(fp, "REFRACTIVE 6 1.000000\n"); 211 | fprintf(fp, "SPECULARCOLOR -1.000000 -1.000000 -1.000000 1.000000\n"); 212 | fprintf(fp, "REFLECTIVECOLOR -1.000000 -1.000000 -1.000000 1.000000\n"); 213 | fprintf(fp, "REFLECTIVE -1 -1.000000\n"); 214 | fprintf(fp, "BLINN -1.000000 -1.000000\n"); 215 | fprintf(fp, "PHONG -1.000000\n"); 216 | } 217 | //if(fp != stdout) 218 | fclose(fp); 219 | return true; 220 | } -------------------------------------------------------------------------------- /animation.cpp: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "util.h" 3 | 4 | void XAnim::read_translations(const std::string& tag) 5 | { 6 | u16 numtrans = m_reader->read(); 7 | if (numtrans == 0) 8 | return; 9 | vec3 v; 10 | if (numtrans == 1) 11 | { 12 | v = m_reader->read(); 13 | //printf("\tnumtrans 1 for '%s' %f,%f,%f\n", tag.c_str(), v.x, v.y, v.z); 14 | m_animframes[0].trans[tag] = v; 15 | return; 16 | } 17 | 18 | std::vector frames; 19 | 20 | if (numtrans == 1 || numtrans == m_numframes) 21 | { 22 | for (int i = 0; i < numtrans; ++i) 23 | frames.push_back(i); 24 | } 25 | else if (m_numframes > 0xff) 26 | { 27 | for (int i = 0; i < numtrans; ++i) 28 | frames.push_back(m_reader->read()); 29 | } 30 | else 31 | { 32 | for (int i = 0; i < numtrans; ++i) 33 | frames.push_back(m_reader->read()); 34 | } 35 | 36 | for (int i = 0; i < numtrans; ++i) 37 | { 38 | v = m_reader->read(); 39 | //printf("trans frame %d -> %f,%f,%f\n", frames[i], v.x, v.y, v.z); 40 | m_animframes[frames[i]].trans[tag] = v; 41 | } 42 | } 43 | 44 | void XAnim::read_rotations(const std::string& tag, bool flipquat, bool simplequat) 45 | { 46 | u16 numrot = m_reader->read(); 47 | //printf("numrot=%d\n", numrot); 48 | if (numrot == 0) 49 | return; 50 | 51 | std::vector frames; 52 | 53 | if (numrot == 1 || numrot == m_numframes) 54 | { 55 | for (int i = 0; i < numrot; ++i) 56 | frames.push_back(i); 57 | } 58 | else if (m_numframes > 0xff) 59 | { 60 | for (int i = 0; i < numrot; ++i) 61 | frames.push_back(m_reader->read()); 62 | } 63 | else 64 | { 65 | for (int i = 0; i < numrot; ++i) 66 | frames.push_back(m_reader->read()); 67 | } 68 | 69 | for (int i = 0; i < numrot; ++i) 70 | { 71 | glm::quat q = m_reader->read_quat(flipquat, simplequat); 72 | //printf("frame %d '%s' -> q = %f,%f,%f,%f flip=%d,simple=%d\n", frames[i], tag.c_str(), q.x, q.y, q.z, q.w, flipquat, simplequat); 73 | if (!flipquat) 74 | { 75 | m_animframes[frames[i]].quats[tag] = q; 76 | } 77 | } 78 | } 79 | 80 | bool XAnim::read_xanim_file(BinaryReader &rd) 81 | { 82 | m_reader = &rd; 83 | m_version = rd.read(); 84 | if (m_version != 0xe) 85 | return rd.set_error_message("expected xanim version 0xe, got %x\n", m_version); 86 | m_numframes = rd.read(); 87 | m_numparts = rd.read(); 88 | m_flags = rd.read(); 89 | m_framerate = rd.read(); 90 | 91 | bool looping = (m_flags & 0x1) == 0x1; 92 | bool delta = (m_flags & 0x2) == 0x2; 93 | m_frequency = ((float)m_framerate) / ((float)m_numframes); 94 | if (delta) 95 | { 96 | read_rotations("tag_origin", false, true); 97 | read_translations("tag_origin"); 98 | } 99 | if (looping) 100 | ++m_numframes; 101 | 102 | std::vector partnames; 103 | 104 | int boneflagssize = ((m_numparts - 1) >> 3) + 1; 105 | std::vector flipflags = rd.read_typed_buffer_to_vector(boneflagssize); 106 | std::vector simpleflags = rd.read_typed_buffer_to_vector(boneflagssize); 107 | for (int i = 0; i < m_numparts; ++i) 108 | { 109 | std::string partname; 110 | if (!rd.read_null_terminated_string(partname)) 111 | break; 112 | partnames.push_back(partname); 113 | } 114 | for (int i = 0; i < m_numparts; ++i) 115 | { 116 | bool flipquat = ((1 << (i & 7)) & flipflags[i >> 3]) != 0; 117 | bool simplequat = ((1 << (i & 7)) & simpleflags[i >> 3]) != 0; 118 | read_rotations(partnames[i], flipquat, simplequat); 119 | read_translations(partnames[i]); 120 | } 121 | std::unordered_map lastpose; 122 | 123 | for (int i = 0; i < m_numframes; ++i) 124 | { 125 | auto fnd = m_animframes.find(i); 126 | XAnimFrame* curframe = NULL; 127 | if (fnd == m_animframes.end()) 128 | { 129 | //printf("skipping frame %d\n", i); 130 | continue; 131 | } 132 | else 133 | { 134 | curframe = &m_animframes[i]; 135 | } 136 | std::vector refframe; 137 | refframe.resize(m_reference->parts.bones.size()); 138 | int refboneindex = 0; 139 | for (auto& refbone : m_reference->parts.bones) 140 | { 141 | Bone xb = refbone; 142 | if (lastpose.find(refbone.name) != lastpose.end()) 143 | { 144 | xb = lastpose[refbone.name]; 145 | } 146 | //xb.trans = glm::rotate(xb.rot, animframe_iterator.second.parts[refbone.name].trans) + xb.trans; 147 | //xb.rot = xb.rot * animframe_iterator.second.parts[refbone.name].rot; 148 | //additive animation 149 | //TODO: FIXME add other relative and global 150 | 151 | //do we have a known good rotation? 152 | if (curframe->quats.find(refbone.name) != curframe->quats.end()) 153 | { 154 | //xb.rot = glm::slerp(xb.rot, curframe->quats[refbone.name], 0.5f); 155 | xb.transform.rotation = curframe->quats[refbone.name]; 156 | } 157 | 158 | //do we have a known good translation? 159 | if (curframe->trans.find(refbone.name) != curframe->trans.end()) 160 | { 161 | //xb.trans = (xb.trans + curframe->trans[refbone.name]) / 2.f; 162 | if (refbone.name == "tag_origin") //ignore any translations for tag_origin, so the animation moves on the spot 163 | ; 164 | else 165 | xb.transform.translation = curframe->trans[refbone.name]; 166 | } 167 | lastpose[refbone.name] = xb; 168 | refframe[refboneindex] = xb; 169 | ++refboneindex; 170 | } 171 | m_refframes.push_back(refframe); 172 | } 173 | u8 notify_count = rd.read(); 174 | for (int i = 0; i < notify_count; ++i) 175 | { 176 | //TODO: write this to xanim_export 177 | std::string notify_string; 178 | rd.read_null_terminated_string(notify_string); 179 | u16 notify_time_value = rd.read(); 180 | printf("notify string: %s:%f, frame: %d\n", notify_string.c_str(), ((float)notify_time_value / ((float)m_numframes)), notify_time_value); 181 | } 182 | return true; 183 | } 184 | 185 | bool XAnim::export_file(const std::string& filename) 186 | { 187 | FILE* fp = NULL; 188 | std::string fullfilename = filename + ".xanim_export"; 189 | fopen_s(&fp, fullfilename.c_str(), "w"); 190 | 191 | if (!fp) 192 | return false; 193 | 194 | fprintf(fp, "// This was file generated with https://github.com/riicchhaarrd/xmodelconverter\n"); 195 | fprintf(fp, "ANIMATION\n"); 196 | fprintf(fp, "VERSION 3\n"); 197 | fprintf(fp, "\n"); 198 | fprintf(fp, "NUMPARTS %d\n", m_reference->parts.bones.size()); 199 | int refboneidx = 0; 200 | for (auto& it : m_reference->parts.bones) 201 | { 202 | fprintf(fp, "PART %d \"%s\"\n", refboneidx++, it.name.c_str()); 203 | } 204 | fprintf(fp, "\n"); 205 | fprintf(fp, "FRAMERATE %d\n", m_framerate); 206 | fprintf(fp, "NUMFRAMES %d\n", m_refframes.size()); 207 | fprintf(fp, "\n"); 208 | int frameno = 0; 209 | for (auto& refframe : m_refframes) 210 | { 211 | fprintf(fp, "FRAME %d\n", frameno++); 212 | refboneidx = 0; 213 | for (auto& xb : refframe) 214 | { 215 | //mat4 mat = refframe[refboneidx].bp;// getWorldMatrix(refframe, refboneidx); 216 | auto mat = util::get_world_transform(refframe, refboneidx).get_matrix(); 217 | vec3 offset = util::get_translation_component_from_matrix(mat); 218 | fprintf(fp, "PART %d\n", refboneidx); 219 | fprintf(fp, "OFFSET %f, %f, %f\n", offset.x, offset.y, offset.z); 220 | fprintf(fp, "SCALE 1.000000, 1.000000, 1.000000\n"); 221 | 222 | vec3 x, y, z; 223 | util::get_xyz_components_from_matrix(mat, x, y, z); 224 | fprintf(fp, "X %f, %f, %f\n", x.x, x.y, x.z); 225 | fprintf(fp, "Y %f, %f, %f\n", y.x, y.y, y.z); 226 | fprintf(fp, "Z %f, %f, %f\n", z.x, z.y, z.z); 227 | fprintf(fp, "\n"); 228 | ++refboneidx; 229 | } 230 | } 231 | fprintf(fp, "NOTETRACKS\n"); 232 | refboneidx = 0; 233 | for (auto& it : m_reference->parts.bones) 234 | { 235 | fprintf(fp, "PART %d\n", refboneidx++); 236 | fprintf(fp, "NUMTRACKS 0\n"); 237 | fprintf(fp, "\n"); 238 | } 239 | fclose(fp); 240 | return true; 241 | } 242 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | #ifdef _WIN32 3 | #include 4 | #endif 5 | 6 | bool BinaryReader::open_path(const std::string& path) 7 | { 8 | auto v = util::read_file_to_memory(path); 9 | if (v.empty()) 10 | return false; 11 | m_buf = v; 12 | m_path = path; 13 | return true; 14 | } 15 | 16 | #ifdef _WIN32 17 | bool get_selected_filepath(std::string &path) 18 | { 19 | OPENFILENAMEA ofn; 20 | char szFile[MAX_PATH + 1]; 21 | // open a file name 22 | ZeroMemory(&ofn, sizeof(ofn)); 23 | ofn.lStructSize = sizeof(ofn); 24 | ofn.hwndOwner = NULL; 25 | ofn.lpstrFile = szFile; 26 | ofn.lpstrFile[0] = '\0'; 27 | ofn.nMaxFile = sizeof(szFile); 28 | ofn.lpstrFilter = "XModel\0*.*\0"; 29 | ofn.nFilterIndex = 1; 30 | ofn.lpstrFileTitle = NULL; 31 | ofn.nMaxFileTitle = 0; 32 | ofn.lpstrInitialDir = NULL; 33 | ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 34 | 35 | if (!GetOpenFileNameA(&ofn)) 36 | return false; 37 | path = ofn.lpstrFile; 38 | return true; 39 | } 40 | #endif 41 | 42 | bool deduce_file_info_from_path(const std::string& path, std::string& basepath, std::string& filepath, int& path_seperator, bool &is_anim) 43 | { 44 | is_anim = false; 45 | #ifndef _WIN32 46 | path_seperator = '/'; 47 | #else 48 | path_seperator = path.find('/') == std::string::npos ? '\\' : '/'; 49 | #endif 50 | 51 | std::string query = "xmodel"; 52 | query += path_seperator; 53 | 54 | int fnd = path.find(query); 55 | if (fnd != std::string::npos) 56 | { 57 | basepath = path.substr(0, fnd); 58 | filepath = path.substr(fnd); 59 | filepath = filepath.substr(filepath.find(path_seperator) + 1); 60 | return true; 61 | } 62 | 63 | query = "xanim"; 64 | query += path_seperator; 65 | 66 | fnd = path.find(query); 67 | if (fnd != std::string::npos) 68 | { 69 | is_anim = true; 70 | basepath = path.substr(0, fnd); 71 | filepath = path.substr(fnd); 72 | filepath = filepath.substr(filepath.find(path_seperator) + 1); 73 | return true; 74 | } 75 | return false; 76 | } 77 | 78 | bool read_model(XModel &xm, const std::string& basepath, const std::string& path, bool &valid_xmodel) 79 | { 80 | BinaryReader rd; 81 | if (!rd.open_path(path)) 82 | { 83 | printf("Failed to read '%s'\n", path.c_str()); 84 | return false; 85 | } 86 | 87 | if (!xm.read_xmodel_file(rd)) 88 | { 89 | printf("Failed to read xmodel file '%s', error: %s\n", path.c_str(), rd.get_error_message().c_str()); 90 | return false; 91 | } 92 | if (xm.lodstrings.empty()) 93 | { 94 | printf("No lods available for exporting for file '%s'\n", path.c_str()); 95 | return false; 96 | } 97 | BinaryReader rd_parts; 98 | if (!rd_parts.open_path(basepath + "xmodelparts/" + xm.lodstrings[0])) 99 | { 100 | printf("Failed to read xmodelparts '%s'\n", xm.lodstrings[0].c_str()); 101 | return false; 102 | } 103 | if (!xm.parts.read_xmodelparts_file(xm, rd_parts)) 104 | { 105 | printf("Failed to parse '%s', error: %s\n", xm.lodstrings[0].c_str(), rd_parts.get_error_message().c_str()); 106 | return false; 107 | } 108 | valid_xmodel = true; 109 | return true; 110 | } 111 | 112 | bool read_animation(XModel &xm, XAnim& xa, const std::string& basepath, const std::string& path, bool &valid_xmodel) 113 | { 114 | BinaryReader rd; 115 | if (!rd.open_path(path)) 116 | { 117 | printf("Failed to read '%s'\n", path.c_str()); 118 | return false; 119 | } 120 | #ifdef _WIN32 121 | if (!valid_xmodel) 122 | { 123 | //for windows, just prompt for the model filepath 124 | 125 | MessageBoxA(NULL, "Please select the xmodel for this xanim.", "Exporter", MB_OK | MB_ICONINFORMATION); 126 | 127 | std::string selected_path; 128 | if (!get_selected_filepath(selected_path)) 129 | { 130 | return false; 131 | } 132 | //TODO: FIXME assuming model & animation have shared basepath 133 | if (!read_model(xm, basepath, selected_path, valid_xmodel)) 134 | { 135 | MessageBoxA(NULL, "Failed to load xmodel.", "Exporter", MB_OK | MB_ICONERROR); 136 | return false; 137 | } 138 | } 139 | #endif 140 | 141 | if (!valid_xmodel) 142 | { 143 | printf("There's no valid xmodel loaded at the moment, please pass a model as reference as argument before the animation.\n"); 144 | return false; 145 | } 146 | xa.m_reference = &xm; 147 | if (!xa.read_xanim_file(rd)) 148 | { 149 | printf("Failed to read xanim file '%s', error: %s\n", path.c_str(), rd.get_error_message().c_str()); 150 | return false; 151 | } 152 | return true; 153 | } 154 | 155 | #include "iwi.h" 156 | 157 | bool convert_iwi_to_dds(const std::string& path, const std::string& export_path) 158 | { 159 | BinaryReader rd; 160 | if (rd.open_path(path)) 161 | { 162 | IWI iwi; 163 | if (iwi.read_from_memory(rd)) 164 | { 165 | std::vector dds; 166 | if (iwi.build_dds(dds)) 167 | { 168 | FILE* fp = 0; 169 | fopen_s(&fp, export_path.c_str(), "wb"); 170 | if (fp) 171 | { 172 | fwrite(dds.data(), 1, dds.size(), fp); 173 | fclose(fp); 174 | return true; 175 | } 176 | else 177 | printf("failed open dds file\n"); 178 | } 179 | else 180 | printf("failed build dds\n"); 181 | } 182 | else 183 | printf("failed read from memory %s\n", rd.get_error_message().c_str()); 184 | } 185 | else 186 | printf("failed to open path '%s'\n", path.c_str()); 187 | return false; 188 | } 189 | 190 | bool get_color_map_from_material_file(const std::string& path, std::string& color_map) 191 | { 192 | BinaryReader rd; 193 | 194 | if (!rd.open_path(path)) 195 | return false; 196 | u32 material_offset = rd.read(); 197 | u32 color_map_offset = rd.read(); 198 | color_map = (char*)(rd.buffer() + color_map_offset); 199 | return true; 200 | } 201 | 202 | int main(int argc, char** argv) 203 | { 204 | std::unordered_map processed; //cache what we exported, so we don't do it again 205 | bool valid_xmodel = false; 206 | XModel ref_xm; 207 | for (int i = 1; i < argc; ++i) 208 | { 209 | std::string path = argv[i]; 210 | 211 | std::string basepath, filepath; 212 | int path_seperator; 213 | bool is_anim; 214 | if (!deduce_file_info_from_path(path, basepath, filepath, path_seperator, is_anim)) 215 | { 216 | printf("Failed to get file information for '%s'\n", path.c_str()); 217 | break; 218 | } 219 | //printf("basepath = %s\n", basepath.c_str()); 220 | //printf("filepath = %s\n", filepath.c_str()); 221 | //getchar(); 222 | XAnim xa; 223 | 224 | if (is_anim) 225 | { 226 | if (!read_animation(ref_xm, xa, basepath, path, valid_xmodel)) 227 | break; 228 | std::string exportpath = basepath; 229 | exportpath += path_seperator; 230 | exportpath += "exported"; 231 | exportpath += path_seperator; 232 | exportpath += filepath; 233 | if (!xa.export_file(exportpath)) 234 | { 235 | printf("Failed exporting animation '%s'\n", filepath.c_str()); 236 | break; 237 | } 238 | printf("Exported animation '%s'\n", filepath.c_str()); 239 | } else 240 | { 241 | XModel xm; 242 | if (!read_model(xm, basepath, path, valid_xmodel)) 243 | break; 244 | //convert all model materials 245 | for (auto& mat : xm.materials) 246 | { 247 | std::string materialpath = basepath + "materials"; 248 | materialpath += path_seperator; 249 | materialpath += mat; 250 | std::string color_map; 251 | get_color_map_from_material_file(materialpath, color_map); 252 | 253 | printf("material: %s, color: %s\n", materialpath.c_str(), color_map.c_str()); 254 | 255 | std::string color_map_path = basepath + "images"; 256 | color_map_path += path_seperator; 257 | color_map_path += color_map + ".iwi"; 258 | std::string color_map_export_path = basepath + "exported"; 259 | color_map_export_path += path_seperator; 260 | color_map_export_path += color_map + ".dds"; 261 | 262 | if (!processed[color_map] && convert_iwi_to_dds(color_map_path, color_map_export_path)) 263 | { 264 | printf("converted %s\n", color_map.c_str()); 265 | processed[color_map] = true; 266 | } 267 | } 268 | for (auto& lod : xm.lodstrings) 269 | { 270 | BinaryReader rd_surfs; 271 | if (!rd_surfs.open_path(basepath + "xmodelsurfs/" + lod)) 272 | { 273 | printf("Failed to read xmodelsurfs '%s'\n", lod.c_str()); 274 | break; 275 | } 276 | if (!xm.surface.read_xmodelsurface_file(xm.parts, rd_surfs)) 277 | { 278 | printf("Failed to parse '%s', error: %s\n", lod.c_str(), rd_surfs.get_error_message().c_str()); 279 | break; 280 | } 281 | 282 | std::string exportpath = basepath; 283 | exportpath += path_seperator; 284 | exportpath += "exported"; 285 | exportpath += path_seperator; 286 | exportpath += lod; 287 | if (!xm.export_file(exportpath)) 288 | { 289 | printf("Failed exporting model '%s'\n", lod.c_str()); 290 | break; 291 | } 292 | printf("Exported model '%s'\n", lod.c_str()); 293 | } 294 | ref_xm = xm; 295 | } 296 | } 297 | #ifdef _WIN32 298 | printf("Press any key to exit.\n"); 299 | getchar(); 300 | #endif 301 | return 0; 302 | } 303 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------