func.pwn 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. LoadTextureCache() {
  2. g_DBResult = db_query(g_ResourceDB, "SELECT * FROM texture_data");
  3. for(new row, rows = db_num_rows(g_DBResult), textureid; row < rows; row ++) {
  4. textureid = db_get_field_assoc_int(g_DBResult, "textureid");
  5. if( IsValidTextureID(textureid) ) {
  6. g_TextureCache[textureid][TEXTURE_CACHE_MODELID] = db_get_field_assoc_int(g_DBResult, "modelid");
  7. db_get_field_assoc(g_DBResult, "txd", g_TextureTXDString, sizeof g_TextureTXDString);
  8. db_get_field_assoc(g_DBResult, "name", g_TextureNameString, sizeof g_TextureNameString);
  9. strpack(g_TextureCache[textureid][TEXTURE_CACHE_TXD], g_TextureTXDString, MAX_TEXTURE_TXD+1);
  10. strpack(g_TextureCache[textureid][TEXTURE_CACHE_NAME], g_TextureNameString, MAX_TEXTURE_NAME+1);
  11. }
  12. db_next_row(g_DBResult);
  13. }
  14. db_free_result(g_DBResult);
  15. }
  16. GetTextureID(modelid, txd[], name[]) {
  17. new textureid = INVALID_TEXTURE_ID;
  18. format(g_QueryString, sizeof g_QueryString,
  19. "SELECT textureid FROM texture_data WHERE modelid = '%i' AND txd = '%q' AND name = '%q' LIMIT 1", modelid, txd, name
  20. );
  21. g_DBResult = db_query(g_ResourceDB, g_QueryString);
  22. if( db_num_rows(g_DBResult) > 0 ) {
  23. textureid = db_get_field_int(g_DBResult);
  24. }
  25. db_free_result(g_DBResult);
  26. return textureid;
  27. }
  28. GetTextureData(textureid, &modelid, txd[], txd_size, name[], name_size) {
  29. if( !IsValidTextureID(textureid) ) {
  30. modelid = -1;
  31. format(txd, txd_size, "none");
  32. format(name, name_size, "none");
  33. return 0;
  34. }
  35. modelid = g_TextureCache[textureid][TEXTURE_CACHE_MODELID];
  36. strunpack(txd, g_TextureCache[textureid][TEXTURE_CACHE_TXD], txd_size);
  37. strunpack(name, g_TextureCache[textureid][TEXTURE_CACHE_NAME], name_size);
  38. return 1;
  39. }
  40. FindTextures(result[], result_size, search[], categoryid, offset, &max_offset) {
  41. new rows_added;
  42. if( categoryid == INVALID_CATEGORY_ID ) {
  43. if( isempty(search) ) {
  44. for(new textureid = offset; textureid < MAX_TEXTURES; textureid ++) {
  45. if( rows_added >= result_size ) {
  46. break;
  47. }
  48. result[rows_added ++] = textureid;
  49. }
  50. max_offset = MAX_TEXTURES - 1;
  51. return rows_added;
  52. } else {
  53. format(g_QueryString, sizeof g_QueryString, "\
  54. SELECT textureid, \
  55. (\
  56. SELECT COUNT(*) \
  57. FROM texture_data \
  58. WHERE textureid LIKE '%%%q%%' OR modelid LIKE '%%%q%%' OR txd LIKE '%%%q%%' OR name LIKE '%%%q%%'\
  59. ) AS max_rowcount \
  60. FROM texture_data \
  61. WHERE textureid LIKE '%%%q%%' OR modelid LIKE '%%%q%%' OR txd LIKE '%%%q%%' OR name LIKE '%%%q%%' \
  62. LIMIT %i OFFSET %i",
  63. search, search, search, search,
  64. search, search, search, search,
  65. result_size, offset
  66. );
  67. }
  68. } else {
  69. if( isempty(search) ) {
  70. format(g_QueryString, sizeof g_QueryString, "\
  71. SELECT textureid, \
  72. (SELECT COUNT(*) FROM texture_category_bind WHERE categoryid = '%i') AS max_rowcount \
  73. FROM texture_category_bind \
  74. WHERE categoryid = '%i' \
  75. LIMIT %i OFFSET %i",
  76. categoryid,
  77. categoryid,
  78. result_size, offset
  79. );
  80. } else {
  81. g_QueryString = "SELECT b.textureid AS textureid,";
  82. format(g_SubQueryString, sizeof g_SubQueryString, "\
  83. (\
  84. SELECT COUNT(*) FROM texture_category_bind b \
  85. INNER JOIN texture_data d ON b.textureid = d.textureid \
  86. WHERE b.categoryid = '%i' AND (d.textureid LIKE '%%%q%%' OR d.modelid LIKE '%%%q%%' OR d.txd LIKE '%%%q%%' OR d.name LIKE '%%%q%%')\
  87. ) AS max_rowcount ",
  88. categoryid, search, search, search, search
  89. );
  90. strcat(g_QueryString, g_SubQueryString);
  91. format(g_SubQueryString, sizeof g_SubQueryString, "\
  92. FROM texture_category_bind b \
  93. INNER JOIN texture_data d ON b.textureid = d.textureid \
  94. WHERE b.categoryid = '%i' AND (d.textureid LIKE '%%%q%%' OR d.modelid LIKE '%%%q%%' OR d.txd LIKE '%%%q%%' OR d.name LIKE '%%%q%%') \
  95. LIMIT %i OFFSET %i",
  96. categoryid, search, search, search, search,
  97. result_size, offset
  98. );
  99. strcat(g_QueryString, g_SubQueryString);
  100. }
  101. }
  102. g_DBResult = db_query(g_ResourceDB, g_QueryString);
  103. new result_rows = db_num_rows(g_DBResult);
  104. if( result_rows > 0 ) {
  105. max_offset = db_get_field_assoc_int(g_DBResult, "max_rowcount") - 1;
  106. if( max_offset < 0 ) {
  107. max_offset = 0;
  108. }
  109. }
  110. for(new row; row < result_rows; row ++) {
  111. if( rows_added >= result_size ) {
  112. break;
  113. }
  114. result[rows_added ++] = db_get_field_assoc_int(g_DBResult, "textureid");
  115. db_next_row(g_DBResult);
  116. }
  117. db_free_result(g_DBResult);
  118. return rows_added;
  119. }