Raised This Month: $ Target: $400
 0% 

[How/To]Adding precache option to plugin.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ticketry
Member
Join Date: Mar 2016
Location: Sacramento, CA
Old 04-20-2016 , 13:39   [How/To]Adding precache option to plugin.
Reply With Quote #1

Howdi all you coder enthusiasts! , Would it be difficult to edit the code of this older plugin to have it precache the (.bmps, .txts) that are usually included with a player model?

I thank you all in advance for those who reply.

-Ticketry

Code:
  #include <amxmodx>
  #include <amxmisc>

  #define ADMINLEVEL ADMIN_IMMUNITY // admin level to bypass model restrictions
  #define MODELFILE "addons/amxmodx/data/modelrestrict.ini"

  new defaultmodel[64] = "gordon"; // default model for connecting users with invalid model
  new validmodel[33][64]; // last used valid model

  // when client changes info
  public client_infochanged(id) {

    // get model
    new model[64];
    get_user_info(id,"model",model,63);

    // if admin, ignore restrictons
    if(access(id,ADMINLEVEL)) {
      validmodel[id] = model;
      return PLUGIN_CONTINUE;
    }

    // check for invalid model
    if(model_isvalid(model) == 0 && model_precache(model) == 0) {
      client_print(id,print_chat,"* Model %s is restricted, changing model back to %s^n",model,validmodel[id]);
      client_cmd(id,"model %s",validmodel[id]);
    }
    else { // is valid
      validmodel[id] = model // store last valid model
    }

    return PLUGIN_CONTINUE;    
  }

  // client connection
  public client_connect(id) {

    // get model
    new model[64];
    get_user_info(id,"model",model,63);

    // if admin, ignore restrictons
    if(access(id,ADMINLEVEL)) {
      validmodel[id] = model;
      return PLUGIN_CONTINUE;
    }

    // check for invalid model
    if(model_isvalid(model) == 0 && model_precache(model) == 0) {
      console_print(id,"* Model %s is restricted, changing model to %s^n",model,defaultmodel);
      client_cmd(id,"model %s",defaultmodel);
      validmodel[id] = defaultmodel;
    }
    else { // is valid
      validmodel[id] = model // store last valid model
    }

    return PLUGIN_CONTINUE;
  }

  // check if a model is valid (unrestricted)
  public model_isvalid(model[64]) {

    // file is non-existant
    if(!file_exists(MODELFILE)) {
      write_file(MODELFILE,"; Avalanche's ModelRestrict",-1); // create it
      return 1; // model is valid because there is no list of invalid models
    }

    new line, text[64], txtlen;

    // loop through file
    while((line = read_file(MODELFILE,line,text,63,txtlen)) != 0) {
      if(equali(text,model)) { // if match
        return 0;
      }
    }

    return 1;
  }

  // check if a model should be precached
  public model_precache(model[64]) {

    // get first 3 characters
    new checkstring[4];
    copy(checkstring,3,model);

    // if starts with "P* "
    if(equali(checkstring,"P* ")) {
      return 1;
    }

    return 0;
  }

  // set a user's model by console command
  public cmd_setmodel(id,level,cid) {
    if(!cmd_access(id,level,cid,3)) { // invalid access
      return PLUGIN_HANDLED;
    }

    // get arguments
    new arg1[64], arg2[64];
    read_argv(1,arg1,63);
    read_argv(2,arg2,63);

    new player = cmd_target(id,arg1,1); // get player

    if(!player) { // invalid player
      return PLUGIN_HANDLED;
    }

    // if admin, ignore restrictons
    if(access(id,ADMINLEVEL)) {
      client_cmd(player,"model %s",arg2);
      validmodel[player] = arg2;
    }
    else { // if regular user
      if(model_isvalid(arg2) == 0 && model_precache(arg2) == 0) { // invalid model
        console_print(id,"* Model %s is restricted, cannot set on target",arg2);
      }
      else { // valid
        client_cmd(player,"model %s",arg2);
        validmodel[player] = arg2;
      }
    }

    return PLUGIN_HANDLED;
  }

  // plugin precache
  public plugin_precache() {

    // file is non-existant
    if(!file_exists(MODELFILE)) {
      write_file(MODELFILE,"; Avalanche's ModelRestrict",-1); // create it
      return PLUGIN_CONTINUE; // no models to precache because there was no file
    }

    new line, text[64], txtlen;

    // loop through file
    while((line = read_file(MODELFILE,line,text,63,txtlen)) != 0) {
      if(model_precache(text) == 1) { // if match
        new leftside[64], rightside[64];
        strbreak(text,leftside,3,rightside,63); // get actual name in right side
        format(leftside,63,"models/player/%s/%s.mdl",rightside,rightside); // put file path in left side

        // couldn't find file
        if(!file_exists(leftside)) {
          server_print("* Could not find model %s to precache",leftside);
        }
        else { // found file
          precache_model(leftside); // precache it
        }
      }
    }

    return PLUGIN_CONTINUE;
  }

  // plugin initiation
  public plugin_init() {
    register_plugin("ModelRestrict","0.11","Avalanche");
    console_print(0,"* Loaded ModelRestrict 0.11 by Avalanche");
    register_concmd("amx_setmodel","cmd_setmodel",ADMINLEVEL,"<target> <model> - set targets's model");
  }
Ticketry is offline
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 04-20-2016 , 14:40   Re: [How/To]Adding precache option to plugin.
Reply With Quote #2

line 143

PHP Code:
// loop through file
while((line read_file(MODELFILE,line,text,63,txtlen)) != 0) {
  if(
model_precache(text) == 1) { // if match
    
new leftside[64], rightside[64];
    
strbreak(text,leftside,3,rightside,63); // get actual name in right side
    
format(leftside,63,"models/player/%s/%s.mdl",rightside,rightside); // put file path in left side

    // couldn't find file
    
if(!file_exists(leftside)) {
      
server_print("* Could not find model %s to precache",leftside);
    }
    else { 
// found file
      
precache_model(leftside); // precache it
    
}
  }

>>

PHP Code:
// loop through file
while((line read_file(MODELFILE,line,text,63,txtlen)) != 0) {
  if(
model_precache(text) == 1) { // if match
    
new leftside[64], rightside[64], bmpFile[64];
    
strbreak(text,leftside,3,rightside,63); // get actual name in right side
    
format(leftside,63,"models/player/%s/%s.mdl",rightside,rightside); // put file path in left side
    
format(bmpFile,63,"models/player/%s/%s.bmp",rightside,rightside); // put file path in left side

    // couldn't find file
    
if(!file_exists(leftside)) {
      
server_print("* Could not find model %s to precache",leftside);
    }
    else { 
// found file
      
precache_model(leftside); // precache it
    
}
    if(!
file_exists(bmpFile)) {
      
server_print("* Could not find bitmap file %s to precache",bmpFile);
    }
    else {
      
precache_generic(bmpFile); // precache it
    
}
  }  

untested

EDIT: Improved, now it will check if the bmp exists before precaching it

This indenting is a headache
__________________

Last edited by gabuch2; 04-20-2016 at 21:38. Reason: Fixed compile errors (missing end brackets ^^'')
gabuch2 is offline
Ticketry
Member
Join Date: Mar 2016
Location: Sacramento, CA
Old 04-20-2016 , 17:31   Re: [How/To]Adding precache option to plugin.
Reply With Quote #3

Quote:
Originally Posted by Shattered Heart Lynx View Post
line 143

PHP Code:
// loop through file
while((line read_file(MODELFILE,line,text,63,txtlen)) != 0) {
  if(
model_precache(text) == 1) { // if match
    
new leftside[64], rightside[64];
    
strbreak(text,leftside,3,rightside,63); // get actual name in right side
    
format(leftside,63,"models/player/%s/%s.mdl",rightside,rightside); // put file path in left side

    // couldn't find file
    
if(!file_exists(leftside)) {
      
server_print("* Could not find model %s to precache",leftside);
    }
    else { 
// found file
      
precache_model(leftside); // precache it
    
}
  }

>>

PHP Code:
// loop through file
while((line read_file(MODELFILE,line,text,63,txtlen)) != 0) {
  if(
model_precache(text) == 1) { // if match
    
new leftside[64], rightside[64], bmpFile[64];
    
strbreak(text,leftside,3,rightside,63); // get actual name in right side
    
format(leftside,63,"models/player/%s/%s.mdl",rightside,rightside); // put file path in left side
    
format(bmpFile,63,"models/player/%s/%s.bmp",rightside,rightside); // put file path in left side

    // couldn't find file
    
if(!file_exists(leftside)) {
      
server_print("* Could not find model %s to precache",leftside);
    }
    else { 
// found file
      
precache_model(leftside); // precache it
    
}
    if(!
file_exists(bmpFile) {
      
server_print("* Could not find bitmap file %s to precache",bmpFile);
    }
    else {
      
precache_generic(bmpFile); // precache it
    
}
  } 
untested

EDIT: Improved, now it will check if the bmp exists before precaching it

This indenting is a headache
I tried giving it a shot and compiling locally and I got afew errors on line:

(160): if(!file_exists(bmpFile) {
(166): if(!file_exists(textFile) {
(169): else {

Here is my code, I tried adding .txt files so those can be downloaded in case.
Code:
    // loop through file
    while((line = read_file(MODELFILE,line,text,63,txtlen)) != 0) {
      if(model_precache(text) == 1) { // if match
        new leftside[64], rightside[64], bmpFile[64], textFile[64];
        strbreak(text,leftside,3,rightside,63); // get actual name in right side
        format(leftside,63,"models/player/%s/%s.mdl",rightside,rightside); // put file path in left side
    format(bmpFile,63,"models/player/%s/%s.bmp",rightside,rightside); // put file path in left side
    format(textFile,63,"models/player/%s/%s.txt",rightside,rightside); // put file path in left side
        
    // couldn't find file
        if(!file_exists(leftside)) {
          server_print("* Could not find model %s to precache",leftside);
        }
        else { // found file
          precache_model(leftside); // precache it
        }
        if(!file_exists(bmpFile) {
          server_print("* Could not find bitmap file %s to precache",bmpFile);
        }
        else {
          precache_generic(bmpFile); // precache it
        }
        if(!file_exists(textFile) {
         server_print("* Could not find text file %s to precache",textFile);
       }
       else {
         precache_generic(textFile); // precache it
        }
      }
    }
Ticketry is offline
gabuch2
AlliedModders Donor
Join Date: Mar 2011
Location: Chile
Old 04-20-2016 , 21:37   Re: [How/To]Adding precache option to plugin.
Reply With Quote #4

Yes, I missed a "}" and a ")" by accident.

I edited my original post it should be working now. (:
__________________
gabuch2 is offline
Ticketry
Member
Join Date: Mar 2016
Location: Sacramento, CA
Old 04-21-2016 , 18:42   Re: [How/To]Adding precache option to plugin.
Reply With Quote #5

Quote:
Originally Posted by Shattered Heart Lynx View Post
Yes, I missed a "}" and a ")" by accident.

I edited my original post it should be working now. (:
It worked out perfectly! It's amazing how tricky coding can be heh, Anyways everything seems to be working perfectly so far and it is precaching the .bmps and everything I totally thank you for the help!
Ticketry is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode