Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precache_player_model #533

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

OciXCrom
Copy link
Contributor

@OciXCrom OciXCrom commented Aug 30, 2018

Since the player model path is always hardcoded (models/player/name/name.mdl) and the functions that set player models demand only the model name instead of the full path, it's nice to have a simple stock to do all the work for us. This stock also checks for a T.mdl file which will prevent a crash if the user didn't add support for this kind of models.

@WPMGPRoSToTeMa
Copy link
Contributor

Why T.mdl is precached here, but not precached in precache_model?

@Arkshine
Copy link
Member

It would be also welcomed to upgrade the existing precache_model to read any model to figure out if there are external textures (T.mdl or t.mdl) and groups (01.mdl, 02.mdl, etc.). I believe ReHLDS already does that now. Then you could base this stock on the upgraded precache_model.

Copy link
Contributor

@WPMGPRoSToTeMa WPMGPRoSToTeMa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Model texture auto-precaching is for another PR. Look into my commits for ReHLDS.

if(file_exists(model))
id = precache_model(model);

replace_string(model, charsmax(model), "T.mdl", ".mdl");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this way, it works because GoldSrc allows only one dot in file path. Imagine if model path is models/player/superT.mdl/superT.mdl.mdl.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa I changed it to check the last characters only

stock precache_player_model(const name[], &id = 0)
{
new model[128];
formatex(model, charsmax(model), "models/player/%s/%sT.mdl", name, name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some models have t.mdl instead of T.mdl, this can be a problem on OS with case-sensitive filesystem (like Linux) rehlds/ReHLDS@6715402.

new model[128];
formatex(model, charsmax(model), "models/player/%s/%sT.mdl", name, name);

if(file_exists(model))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the way how it should be checked, you need to check if model really needs it rehlds/ReHLDS@9080b9b#diff-462e3f794ed90c12c86f5d8d5b725c4aR5405.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa Which I assume can't be done with a stock?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OciXCrom it can be done through the reading of model file.

formatex(model, charsmax(model), "models/player/%s/%sT.mdl", name, name);

if(file_exists(model))
id = precache_model(model);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to precache it via precache_generic, because precache_generic doesn't have limit like precache_model (on ReHLDS).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa I changed it to generic.

@OciXCrom
Copy link
Contributor Author

@Arkshine @WPMGPRoSToTeMa I'll leave it up to you guys to update precache_model because I don't think I'll be able to do it. I'll stick to the stock.


replace_string(model, charsmax(model), "T.mdl", ".mdl");
replace_string(model[strlen(model) - 4], charsmax(model), "T.mdl", ".mdl");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not the best option, magic 4 should be replaced with something like sizeof("T.mdl") - 1. But anyway replace_string doesn't fit good for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa I changed it to charsmax because sizeof would output 6 instead.

What do you suggest using instead replace_string? I couldn't figure out a way to manually insert a character without using functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OciXCrom copy maybe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa I changed it to copy.

@Arkshine
Copy link
Member

Will do it later if you can't.

if(file_exists(model))
id = precache_generic(model);

static const extension[] = "T.mdl";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why static ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justgo97 It doesn't need to be defined more than once. It acts like a global variable with a local scope.

Copy link

@justgo97 justgo97 Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static is bad here because its lifetime (or "extent") is the entire run of the program while these stock will be only used in one forward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static is fully OK for local array/string constants. There is no other option:

  1. new isn't appropriate here because it does copying on each function call.
  2. Global constant is bad because we use this constant only in this function, so there is no reason for it to be global.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but this string constant is pretty small in size and in its use having it static means it will stay in memory all the time while its only needed at the 1-2 seconds of map start for plugin_precache forward ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justgo97 no, new = data + stack, static = data.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa I mean in overall which is better for memory ?

public plugin_init()
{
    func1()

    func2()
}

public func1()
{
    new const Block1[] = "I_M_GOING_TO_STAY_IN_MEMORY_JUST_FOR_THIS_FUNC"
    new const Block2[] = "I_M_GOING_TO_STAY_IN_MEMORY_JUST_FOR_THIS_FUNC"
    new const Block3[] = "I_M_GOING_TO_STAY_IN_MEMORY_JUST_FOR_THIS_FUNC"
    new const Block4[] = "I_M_GOING_TO_STAY_IN_MEMORY_JUST_FOR_THIS_FUNC"
    new const Block5[] = "I_M_GOING_TO_STAY_IN_MEMORY_JUST_FOR_THIS_FUNC"
    new const Block6[] = "I_M_GOING_TO_STAY_IN_MEMORY_JUST_FOR_THIS_FUNC"
}

public func2()
{
    static const Block1[] = "I_M_GOING_TO_STAY_IN_MEMORY_FOR_EVER"
    static const Block2[] = "I_M_GOING_TO_STAY_IN_MEMORY_FOR_EVER"
    static const Block3[] = "I_M_GOING_TO_STAY_IN_MEMORY_FOR_EVER"
    static const Block4[] = "I_M_GOING_TO_STAY_IN_MEMORY_FOR_EVER"
    static const Block5[] = "I_M_GOING_TO_STAY_IN_MEMORY_FOR_EVER"
    static const Block6[] = "I_M_GOING_TO_STAY_IN_MEMORY_FOR_EVER"
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justgo97 if consider only permanent memory, then they are both the same.

Copy link

@justgo97 justgo97 Aug 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WPMGPRoSToTeMa so for non-permanent memory new is better, which is an advantage for new over static ?

Copy link
Contributor

@rsKliPPy rsKliPPy Aug 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter, you shouldn't spend time deciding that while coding.

@OciXCrom
Copy link
Contributor Author

@Arkshine Sounds good.

@WPMGPRoSToTeMa
Copy link
Contributor

I think support for model texture should be done not in precache_model, but at some other side, like PF_precache_model_I hook in order to precache model texture for all models, not only for precached from plugins (e.g. from amxx modules).

@Arkshine
Copy link
Member

That would be ideal, but not everyone uses ReHLDS and we can't expect any additional features from Valve.

@@ -921,6 +921,7 @@ stock precache_player_model(const name[], &id = 0)
if(file_exists(model))
id = precache_generic(model);

replace_string(model[strlen(model) - 5], charsmax(model), "T.mdl", ".mdl");
static const extension[] = "T.mdl";
replace_string(model[charsmax(extension)], charsmax(model), extension, ".mdl");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charsmax isn't named good for this case, it would be better to have something like const_strlen.

@WPMGPRoSToTeMa
Copy link
Contributor

@Arkshine we can implement it in amxmodx for non-ReHLDS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants